gpt4 book ai didi

android - 像主屏幕滑动动画一样的 ImageSwitcher 动画

转载 作者:行者123 更新时间:2023-11-30 01:43:33 25 4
gpt4 key购买 nike

这是一个简单的应用程序,可以通过右/左糖果在图像之间切换

布局:

<?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;
}
});
}

手势管理器

    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 还是有其他更适合此应用程序的 View ?

(我尝试在 MotionEvent.ACTION_MOVE 中应用动画,但我不确定这是否是一个好的解决方案。)

谢谢你的帮助

最佳答案

使用 ViewPager 尝试屏幕幻灯片

ViewPagers have built-in swipe gestures to transition through pages, and they display screen slide animations by default, so you don't need to create any

关于android - 像主屏幕滑动动画一样的 ImageSwitcher 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34084062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com