android 補間動畫TranslateAnimation

做項目的時候遇見了隨着按鍵的移動,Imageview 直線的移動。第一次寫,寫的不好,忘見諒。

TranslateAnimation只是水平上下移動。

imageview設置設置matrix,然後圖片開啓動畫。

這是MainActiviry裏面的

package com.example.animationdemo;

import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity implements OnClickListener
{

	private Button btn1;
	private Button btn2;
	private Button btn3;

	// 動畫圖片
	private ImageView image;
	// 動畫圖片偏移量
	private int offset = 0;
	// 當前頁卡編號
	private int currIndex = 0;
	// 動畫圖片寬度
	private int bmpW;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn1 = (Button) findViewById(R.id.txt_bar_home_page);
		btn2 = (Button) findViewById(R.id.txt_bar_smart_e_home);
		btn3 = (Button) findViewById(R.id.txt_bar_interaction);
		
		image = (ImageView) findViewById(R.id.img_bar_line);

		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
		btn3.setOnClickListener(this);
		bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.line_toolsbar).getWidth();// 獲取圖片寬度
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;// 獲取分辨率寬度
		offset = (screenW / 3 - bmpW) / 2;// 計算偏移量
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0); //設置 初始位置
		image.setImageMatrix(matrix);// 設置動畫初始位置
	}

	private void transAnim(int id)
	{
		int one = offset * 2 + bmpW;// 頁卡1 -> 頁卡2 偏移量
		int two = one * 2;// 頁卡1 -> 頁卡3 偏移量

		Animation animation = null;
		switch (id)
		{
		case 0:
			if (currIndex == 1)
			{
				animation = new TranslateAnimation(one, 0, 0, 0);
			} else if (currIndex == 2)
			{
				animation = new TranslateAnimation(two, 0, 0, 0);
			}
			break;
		case 1:
			if (currIndex == 0)
			{
				animation = new TranslateAnimation(offset, one, 0, 0);
			} else if (currIndex == 2)
			{
				animation = new TranslateAnimation(two, one, 0, 0);
			}
			break;
		case 2:
			if (currIndex == 0)
			{
				animation = new TranslateAnimation(offset, two, 0, 0);
			} else if (currIndex == 1)
			{
				animation = new TranslateAnimation(one, two, 0, 0);
			}
			break;
		}
		if (animation != null)
		{
			currIndex = id;
			animation.setFillAfter(true); //  True:圖片停在動畫結束位置      保持結束後的效果
			animation.setDuration(300);   //  設置執行時間
			animation.setRepeatCount(2);   //設置 執行次數
			image.startAnimation(animation);

		}
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.txt_bar_home_page:
			transAnim(0);
			break;
		case R.id.txt_bar_smart_e_home:
			transAnim(1);
			break;
		case R.id.txt_bar_interaction:
			transAnim(2);
			break;
		default:
			break;
		}
	}
}


佈局文件中的

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.animationdemo.MainActivity" >

    <ImageView
        android:id="@+id/img_bar_line"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:scaleType="matrix"
        android:src="@drawable/line_toolsbar" />
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/img_bar_line"
        android:layout_marginTop="3dp"
        android:orientation="horizontal">
        <Button 
            android:layout_weight="1"
            android:id="@+id/txt_bar_home_page"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我是一"/>
        <Button 
             android:layout_weight="1"
            android:id="@+id/txt_bar_smart_e_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我是二"/>
        <Button 
             android:layout_weight="1"
            android:id="@+id/txt_bar_interaction"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我是三"/>
    </LinearLayout>

</RelativeLayout>

圖片 沒做出來,不好意思 下次貼圖把

發佈了35 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章