【问题标题】:Android animation with an ImageView带有 ImageView 的 Android 动画
【发布时间】:2023-04-04 09:28:02
【问题描述】:

我有一个相框,应该在其中添加一张图片进行动画处理。我将使用一个 imageView,其中我的背景将是框架,而我的图片将是前景图像。我必须以一种相框从顶部落入屏幕中间的方式来展示它,并在其顶部附有一条丝带。将其放入中心后,只有带有图像的框架进行 toFro 运动,而色带保持刚性并且不移动。我该怎么做这样的动画?

谢谢

【问题讨论】:

    标签: android android-layout animation android-animation android-2.2-froyo


    【解决方案1】:

    在您的 Activity 类中声明这些变量

    private ImageView myFrameAnimation;
    private int iFrameCount;
    private ImageView myPhotoLayer;
    

    使用 onStart 或按钮点击事件启动动画

    @Override
    protected void onStart() {
        super.onStart();
        Thread thr1 = new Thread(r1);
        thr1.start();
    }
    

    然后创建一个新线程来做动画

    Runnable r1 = new Runnable() {
        public void run() {
        while (iFrameCount< 747) {
    
            runOnUiThread(new Runnable() {
                public void run() {
                    iFrameCount++;
                    switch (iFrameCount) {
                        //you can use a separate procedure here to move the ImageView with your picture in time with your animation
                        //the x,y cordinates ‎l depend on your screen/View size measured from top left corner. 
                    case 310: animate_object(330,215,310,215,1);
                    case 315: animate_object(150,380,150,420,80);
                    case 320: animate_object(80,150,80,120,100);
                    }
    
                       //store your animation images in the drawables folder with the name like img1.png, img2.png etc.     
                    String image="img" + iFrameCount;
                    resID = getResources().getIdentifier(image, "drawable",  getPackageName()); 
                    if (resID != 0){
    
                        //Use these to time how long it takes to update the imageview/ Set your thread.sleep to around this time.
                        //startTime = System.currentTimeMillis();                           
                     myFrameAnimation.setImageResource(resID);
                        //endTime = System.currentTimeMillis();
                        //Log.d("Frame: " +iFrameCount + " setBackground took: " + (endTime - startTime) + " milliseconds");
    
                    } else {
                               //we can skip frames if they are duplicates of the frame before to save space and memory and increase speed
                         //Log.d(TAG, "File skipped: " + iFrameCount);
                    }
                 }
               });
            try {
               Thread.sleep(43); //43 is milliseconds to give around 23 frames per second speed will depend on your image size that you are animating.
            } catch (InterruptedException iex) {}
          }           
          //Log.d(TAG, "Finished all frames");
          finish();
       }
    };
    

    最后有一个程序可以将照片与相框一起制作动画

    public void animate_object(int startx, int starty, int endx, int endy, int duration)
    {
    //photoLayer is the Id of your imageView holding the photo. You would need to have previously loaded your photo into the imageview using
    //something like this myPhotoLayer.setImageResource(resID); before the animation is started.
    
        myPhotoLayer = (ImageView) findViewById(R.id.photoLayer); 
        TranslateAnimation move = new TranslateAnimation(startx, endx, starty, endy);
        move.setDuration(duration);
        move.setFillAfter(true);
        myPhotoLayer.startAnimation(move);
    }
    

    【讨论】:

    • 我编辑了一些我使用的现有代码,所以希望我把所有的括号都放在适合你的地方。
    【解决方案2】:

    你需要把它画成卡通。首先尝试对其进行视频制作以获得您需要的运动,然后创建一个 PNG 序列(使用 Adob​​e After Effects),将您想要的帧作为指导,然后在 Illustrator 或 Photoshop 中重新绘制它们。

    然后使用animation.drawable创建一个动画来显示帧,只要它没有太多帧。

    【讨论】:

    • 谢谢,但我需要在运行时集成图片,即从画廊或相机拍照,然后将其添加到动画中。所以这可能对我不起作用。
    • 嗨,小伙子,您要选择照片还是拍照然后让它出现在屏幕上的相框中?如果是这种情况,您将创建我上面提到的动画。首先拍摄它以获得运动。然后在动画程序(如 Flash 或 After Effects)中绘制它。创建帧的一系列 PNG 图像(如卡通)。使尺寸尽可能小(不是全屏)。那么……
    • 要么使用animation.drawable(如果它足够小),要么循环切换PNG(我就是这样做的),然后让你的图片在不同的Imageview中设置动画图片帧动画层,然后逐帧移动顶部的图像视图,使其在下降和摆动时留在帧内。应该这样做。
    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多