做项目的时候遇见了随着按键的移动,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>
图片 没做出来,不好意思 下次贴图把