2

2つの間をスライドするためのこのコードがあります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ユーザーが画面上で指をスライドさせ、指の動きに「追従」 できるように、このコードを変更するにはどうすればよいですか?

4

2 に答える 2

3

android.support.v4.view.ViewPager をアダプターで実装するだけで、非常に簡単です。これはアダプタ クラスのコードです。ビューを に追加し、のアダプタをこのアダプタに adapter設定するだけです。ViewPager

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

}

于 2012-09-19T13:12:13.107 に答える
1

ユーザーが画面上で指をスライドできるように、このコードを変更するにはどうすればよいですか?

SwingのMouseDragListenerに相当するものをどこかに追加します。おそらく誰かがそのイベント名を教えてくれるでしょう(多分MotionEvent. ACTION_MOVE)。とにかく、あなたのアクティビティへのコールバックを持っているという考えです。そうすれば、x1、y1、x2、y2座標を持っていると「フォロー」するのが簡単になります

于 2012-09-19T13:01:29.447 に答える