滚动图像时的Android动画

时间:2015-04-23 08:38:51

标签: android

我在创建动画方面遇到问题,我们在Android上的Box应用程序中有这个问题。 我尝试了一切,但无法在下一个视图上旋转图像。

所以,如果你的人可以提供一些指导.....将非常感激。

这是代码 这是我用来滑动图像的类

公共类CustomHorizo​​ntalScrollView扩展了Horizo​​ntalScrollView实现         OnTouchListener,OnGestureListener,android.view.GestureDetector.OnGestureListener {

private static final int SWIPE_MIN_DISTANCE = 300;

private static final int SWIPE_THRESHOLD_VELOCITY = 300;

private static final int SWIPE_PAGE_ON_FACTOR=50;

private GestureDetector gestureDetector;
private int scrollTo = 0;
private int maxItem = 0;
private int activeItem = 0;
private float prevScrollX = 0;
private boolean start = true;
private int itemWidth = 0;
private float currentScrollX;
private boolean flingDisable = true;

public CustomHorizontalScrollView(Context context) {
    super(context);
    setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));
}

public CustomHorizontalScrollView(Context context, int maxItem,
        int itemWidth) {
    this(context);
    this.maxItem = maxItem;
    this.itemWidth = itemWidth;
    gestureDetector = new GestureDetector(this);
    this.setOnTouchListener(this);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (gestureDetector.onTouchEvent(event)) {
        return true;
    }
    Boolean returnValue = gestureDetector.onTouchEvent(event);

    int x = (int) event.getRawX();

    switch (event.getAction()) {
    case MotionEvent.ACTION_MOVE:
        if (start) {
            this.prevScrollX = x;
            start = false;
        }
        break;
    case MotionEvent.ACTION_UP:
        start = true;
        this.currentScrollX = x;
        int minFactor = itemWidth/ SWIPE_PAGE_ON_FACTOR;

        if ((this.prevScrollX - this.currentScrollX) > minFactor) {
            if (activeItem < maxItem - 1)
                activeItem = activeItem + 1;

        } else if ((this.currentScrollX - this.prevScrollX) > minFactor) {
            if (activeItem > 0)
                activeItem = activeItem - 1;
        }
        System.out.println("horizontal : " + activeItem);
        scrollTo = activeItem * itemWidth;
        this.smoothScrollTo(scrollTo, 0);
        returnValue = true;
        break;
    }
    return returnValue;
}

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    if (flingDisable)
        return false;
    boolean returnValue = false;
    float ptx1 = 0, ptx2 = 0;
    if (e1 == null || e2 == null)
        return false;
    ptx1 = e1.getX();
    ptx2 = e2.getX();
    // right to left

    if (ptx1 - ptx2 > SWIPE_MIN_DISTANCE
            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        if (activeItem < maxItem - 1)
            activeItem = activeItem + 1;

        returnValue = true;

    } else if (ptx2 - ptx1 > SWIPE_MIN_DISTANCE
            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        if (activeItem > 0)
            activeItem = activeItem - 1;

        returnValue = true;
    }
    scrollTo = activeItem * itemWidth;
    this.smoothScrollTo(0, scrollTo);
    return returnValue;
}

public boolean onDown(MotionEvent e) {
    return false;
}

public void onLongPress(MotionEvent e) {
}

public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {

    return Math.abs(distanceY) > Math.abs(distanceX);
}

public void onShowPress(MotionEvent e) {
}

public boolean onSingleTapUp(MotionEvent e) {
    return false;
}

@Override
public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
    // TODO Auto-generated method stub

}

@Override
public void onGesture(GestureOverlayView overlay, MotionEvent event) {
    // TODO Auto-generated method stub

}

@Override
public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
    // TODO Auto-generated method stub

}

@Override
public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
    // TODO Auto-generated method stub

}

}

主要活动

公共类MainActivity扩展了Activity {

 private LinearLayout linearLayout;
 private CustomHorizontalScrollView horizontalScrollView;

 int imgid[] = {  R.drawable.logo2, R.drawable.logo3,
            R.drawable.logo4,};

 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

 int width = MainActivity.this.getWindowManager().getDefaultDisplay().getWidth();
 int height = MainActivity.this.getWindowManager().getDefaultDisplay().getHeight();
  horizontalScrollView = new CustomHorizontalScrollView(this, 3, width);
  setContentView(R.layout.activity_main);
  linearLayout = (LinearLayout) findViewById(R.id.layer);
  linearLayout.addView(horizontalScrollView);

  LinearLayout container = new LinearLayout(this);
  container.setLayoutParams(new LayoutParams(width, height));
  // container.setHeight(height);

  TextView textView = new TextView(this);
  textView.setWidth(width);
 // textView.setHeight(height);
  textView.setGravity(Gravity.CENTER);

  textView.setBackgroundResource(R.drawable.logo2);
  container.addView(textView);

  textView = new TextView(this);
  textView.setWidth(width);
  textView.setHeight(height);
  textView.setGravity(Gravity.CENTER);

  textView.setBackgroundResource(R.drawable.logo3);
  container.addView(textView);

  textView = new TextView(this);
  textView.setWidth(width);
  textView.setHeight(height);
  textView.setGravity(Gravity.CENTER);


  textView.setBackgroundResource(R.drawable.logo4);
  container.addView(textView); 

 // ImageView imageView=new ImageView(this);
    //imageView.setImageResource(R.drawable.logo3);
    //imageView.setImageResource(R.drawable.logo4);
    //imageView.setAdjustViewBounds(true);
//  imageView.setScaleType(ScaleType.FIT_XY);
//  container.addView(imageView);

    horizontalScrollView.addView(container);

 }

}

=========================== 上面的片段可以刷图像,但我想将图像旋转到90度,就像垂直到水平一样,并且想要将相同的图像移动到具有适当轴的下一个视图....这就是我想要的...我是新手机器人。 提前谢谢。

0 个答案:

没有答案