这是一个简单的应用程序,可以通过右/左糖果切换图像
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageSwitcher
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageSwitcher"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:background="@drawable/s"
/>
</RelativeLayout>
onCreate中的定义:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sw = (ImageSwitcher) findViewById(R.id.imageSwitcher);
sw.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView myView = new ImageView(getApplicationContext());
myView.setImageResource(R.drawable.s);
myView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
GestureManager.onTouch(v , event);
return true;
}
});
myView.setScaleType(ImageView.ScaleType.FIT_CENTER);
myView.setLayoutParams(new ImageSwitcher.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return myView;
}
});
}
GestureManager
static boolean onTouch(View v, MotionEvent event) {
int pointerId = event.getPointerId(event.getActionIndex());
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:{
pointerList.clear();
mPoints.clear();
mPoints.add(new MyPoint(pointerId , event.getX() , event.getY()));
break;
}
case MotionEvent.ACTION_MOVE: {
for (int size = event.getPointerCount(), i = 0; i < size; i++) {
mPoints.add(new MyPoint(event.getPointerId(i) , event.getX(i) , event.getY(i)));
}
break;
}
case MotionEvent.ACTION_UP:
processGesture(v,true);
break;
}
return true;
}
private static void processGesture(View v,boolean singleGes){
if(pointerList.size()==1){
String msg = detectSingleGesture(mPoints);
int size_x=MainActivity.sw.getWidth();
switch (msg) {
case "left":
TranslateAnimation left_to_right_in = new TranslateAnimation( size_x,0 , 0, 0 );
left_to_right_in.setDuration(1000);
left_to_right_in.setZAdjustment(AnimationSet.ZORDER_TOP);
TranslateAnimation left_to_right_out = new TranslateAnimation( 0,-size_x, 0, 0 );
left_to_right_out.setDuration(1000);
left_to_right_out.setZAdjustment(AnimationSet.ZORDER_BOTTOM);
MainActivity.sw.setInAnimation(left_to_right_in);
MainActivity.sw.setOutAnimation(left_to_right_out);
index--;
break;
case "right":
TranslateAnimation right_left_in = new TranslateAnimation( -size_x,0 , 0, 0 );
right_left_in.setDuration(1000);
right_left_in.setZAdjustment(AnimationSet.ZORDER_TOP);
TranslateAnimation right_left_in_out = new TranslateAnimation( 0,size_x, 0, 0 );
right_left_in_out.setDuration(1000);
right_left_in_out.setZAdjustment(AnimationSet.ZORDER_BOTTOM);
MainActivity.sw.setInAnimation(right_left_in);
MainActivity.sw.setOutAnimation(right_left_in_out);
index++;
break;
}
if(index>4) index=0;
if(index<0) index=4;
// MainActivity.sw.setImageResource(image_ids[index]);
Drawable d=new BitmapDrawable(context.getResources(), bmp[index]);
MainActivity.sw.setImageDrawable(d);
}
}
此代码正常工作,通过向左或向右扫描动画运行并将图像转换为新动画
问题:
如何根据手指在屏幕上的移动来修改此代码以使新图像移动,就像android主屏幕一样?
我还应该使用ImageSwitcher,还是有其他的视图更适合这个应用程序?
(我尝试在MotionEvent.ACTION_MOVE
中应用动画,但我不确定它是不是一个好的解决方案。)
感谢您的帮助