gpt4 book ai didi

android - ViewFlipper 动画,在两个 ImageView 之间平滑滚动

转载 作者:搜寻专家 更新时间:2023-11-01 08:08:08 25 4
gpt4 key购买 nike

我有这段代码可以在两个 ImageViews 之间滑动

public class CaseHolder extends MainClass{

// for the next movement
public static Animation inFromLeftAnimation() {
Animation inFromLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromLeft.setDuration(350);
inFromLeft.setInterpolator(new AccelerateInterpolator());
return inFromLeft;
}
// for the previous movement
public static Animation inFromRightAnimation() {

Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromRight.setDuration(350);
inFromRight.setInterpolator(new AccelerateInterpolator());
return inFromRight;
}
public static Animation outToLeftAnimation() {
Animation outtoLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoLeft.setDuration(350);
outtoLeft.setInterpolator(new AccelerateInterpolator());
return outtoLeft;
}
public static Animation outToRightAnimation() {
Animation outtoRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoRight.setDuration(350);
outtoRight.setInterpolator(new AccelerateInterpolator());
return outtoRight;
}
TextView pcn;

TextView header;
TextView date;
ImageView right;
TextView status;

ImageView left;
String dateFromIntent;

String pcnFromIntent;

String statusFromIntent;

ViewFlipper vf;

float oldTouchValue;

boolean isDown;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setTabBar(R.layout.caseholder);
overridePendingTransition(R.anim.activityfade, R.anim.activityfadeout);

vf = (ViewFlipper) findViewById(R.id.viewFlipper01);


pcn = (TextView) findViewById(R.id.pcn);
header = (TextView) findViewById(R.id.textView1);
date = (TextView) findViewById(R.id.date);
status = (TextView) findViewById(R.id.status);

right = (ImageView) findViewById(R.id.rightF);
left = (ImageView) findViewById(R.id.leftF);

header.setText("Overview");
pcnFromIntent = getIntent().getExtras().getString("pcn");
dateFromIntent = getIntent().getExtras().getString("date");
statusFromIntent = getIntent().getExtras().getString("status");

date.setText("Date: " + dateFromIntent);
pcn.setText(pcnFromIntent);
status.setText("Status: " + statusFromIntent);

if (getIntent().hasExtra("right")) {
Bitmap b = BitmapFactory.decodeByteArray(getIntent()
.getByteArrayExtra("right"), 0, getIntent()
.getByteArrayExtra("right").length);
right.setImageBitmap(b);
}

if (getIntent().hasExtra("left")) {
Bitmap b1 = BitmapFactory.decodeByteArray(getIntent()
.getByteArrayExtra("left"), 0, getIntent()
.getByteArrayExtra("left").length);

left.setImageBitmap(b1);
}

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent j = new Intent(getApplicationContext(), MainClass.class);
startActivity(j);
CaseHolder.this.finish();
overridePendingTransition(R.anim.activityfade, R.anim.activityfadeout);
return true;
}
return super.onKeyDown(keyCode, event);
}

@Override
public boolean onTouchEvent(MotionEvent touchevent) {

switch (touchevent.getAction()) {
case MotionEvent.ACTION_DOWN: {
oldTouchValue = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP: {

float currentX = touchevent.getX();
if (oldTouchValue < currentX) {
vf.setInAnimation(inFromLeftAnimation());
vf.setOutAnimation(outToRightAnimation());
vf.showNext();

}
if (oldTouchValue > currentX) {
vf.setInAnimation(inFromRightAnimation());
vf.setOutAnimation(outToLeftAnimation());
vf.showPrevious();
}
break;
}
}
return false;
}

这完全没问题,但我想要更平滑的滚动。目前,用户必须将手指从屏幕上移开,才能在 Views 之间滑动。我如何修改这段代码,以便用户可以在屏幕上滑动手指,而 ViewFlipper 将“跟随”手指的移动?

最佳答案

只需使用其适配器实现 android.support.v4.view.ViewPager,很简单。这是适配器类的代码。您只需将 View 添加到 adapter 并将 ViewPager 的 Adapter 设置为此。

public class PreferencesViewPagerAdapter extends PagerAdapter
{
ArrayList<View> preferencesViews;

public PreferencesViewPagerAdapter(ArrayList<View> preferencesViews)
{
this.preferencesViews = preferencesViews;
}

@Override
public Object instantiateItem(ViewGroup container, int position)
{
((ViewPager) container).addView(preferencesViews.get(position));
return preferencesViews.get(position);
}

@Override
public int getCount()
{
return preferencesViews.size();
}

@Override
public boolean isViewFromObject(View view, Object object)
{
return view == object;
}

@Override
public void destroyItem(ViewGroup container, int position, Object view)
{
((ViewPager) container).removeView((View) view);
}

关于android - ViewFlipper 动画,在两个 ImageView 之间平滑滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12495158/

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