繁体   English   中英

android在ImageView动画中放置图像

[英]android Position an image inside an ImageView animation

我有imageView

<ImageView
                android:id="@+id/loading"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:scaleType="centerCrop"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/spinner"/>

和图像是 在此处输入图片说明

我想创建一个动画来改变imageView中图像的位置

我尝试了这个

android:scrollX="-80dp"

这在XML和静态我想要动画

您可以通过创建一个线程和一个处理程序来做到这一点,该线程在该线程中休眠一定时间,并在唤醒时通过发送消息通知处理程序,并且处理程序将更新图像视图中的图像。

创建如下四个字段:

private ImageView mImageView;
private Handler mImageChangingHandler;
private int mCurrentImageIndex = 0;
private BackgroundThread mImageUpdateThread;

onCreate()获取ImageView,溢出您的位图并将其添加到列表中,并创建一个更新imageview的处理程序。 在这里,您应该知道主Bitmap 中单个位图数量 喜欢 :

mImageView = (ImageView) findViewById(R.id.img);
Bitmap mainBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.anim);
//number of individual bitmaps, which you should know.
final int numberOfImages = 21;
//individual bitmap width
int bitmapWidth = mainBitmap.getWidth() / numberOfImages;
//individual bitmap height
int bitmapHeight = mainBitmap.getHeight();
//list which holds individual bitmaps.
final List<Bitmap> animBitmaps = new ArrayList<>(numberOfImages);
//split your bitmap in to individual bitmaps
for (int index = 0; index < numberOfImages; index++) {
    animBitmaps.add(Bitmap.createBitmap(mainBitmap, index * bitmapWidth, 0, bitmapWidth, bitmapHeight));
}
//set 1st bitmap to imageView.
mImageView.setImageBitmap(animBitmaps.get(mCurrentImageIndex));
mImageChangingHandler = new Handler(new Handler.Callback() {
    @Override
    public boolean handleMessage(Message msg) {
        //increament current bitmap index
        mCurrentImageIndex++;
        //if current index is greater than the number of images, reset it to 0
        if (mCurrentImageIndex > numberOfImages - 1) {
            mCurrentImageIndex = 0;
        }
            mImageView.setImageBitmap(animBitmaps.get(mCurrentImageIndex));
            return true;
    }
});
//Create the background thread by passing the handler and start.
mImageUpdateThread = new BackgroundThread(mImageChangingHandler);
mImageUpdateThread.setRunning(true);
mImageUpdateThread.start();

这将是您的BackgroundThread.class

public static class BackgroundThread extends Thread {

    private Handler mHandler;
    private boolean mRunning;

    public BackgroundThread(Handler handler) {
        mHandler = handler;
    }

    public void setRunning(boolean running) {
        mRunning = running;
    }

    @Override
    public void run() {
        super.run();
        while (mRunning) {
            try {
                //this sleeps for 100 milliseconds
                sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                //send message to handler after sleep
                mHandler.sendEmptyMessage(0);
            }
        }
    }
}

最后在onDestroy()停止线程。 喜欢 :

mImageUpdateThread.setRunning(false);
boolean stopThread = true;
    while (stopThread) {
        try {
            mImageUpdateThread.join();
            //thread already stopped
            stopThread = false;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
mImageUpdateThread = null;

注意:如果要提高更新速度,请减少线程中的睡眠时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM