android在ImageView动画中定位图像

时间:2017-01-23 12:20:12

标签: android android-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"/>

和图像是 enter image description here

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

我试过这个

android:scrollX="-80dp"

这在xml和静态中我想要动画

1 个答案:

答案 0 :(得分:1)

你可以通过创建一个线程和一个处理程序来执行它,其中线程会休眠一段时间,并通过在唤醒时发送消息来通知处理程序,处理程序将在图像视图中更新图像。

创建4个字段,如下所示:

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

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

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;

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