Android降低图像高度会留下痕迹

时间:2014-09-30 06:42:13

标签: android android-imageview image-scaling

我尝试使用单点触摸为我的应用设置图像视图的高度。我希望它在我向上滑动时降低图像高度,并在向下滑动时增加。这是我的代码:

public class MainActivity extends Activity {

    private static final String DEBUG_TAG = "MainActivity";
    private VelocityTracker mVelocityTracker = null;
    LinearLayout gameBoardView;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gameBoardView = (LinearLayout) findViewById(R.id.gameBoard);
        initCardFromTop();
    }

    private void initCardFromTop() {
        Log.e("MainActivity: initCardFromTop()", "called");
        final ImageView ivTopCard = new ImageView(MainActivity.this);
        ivTopCard.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        ivTopCard.setImageResource(R.drawable.up_arrow);
        ivTopCard.setBackgroundColor(getResources().getColor(R.color.emerald));
//        ivTopCard.setScaleType(ImageView.ScaleType.FIT_CENTER);
//        ivTopCard.setAdjustViewBounds(true);
//        ivTopCard.setBackgroundResource(R.drawable.up_arrow);
        ivTopCard.setOnTouchListener(new ImageTouchListener());
        gameBoardView.addView(ivTopCard);
    }

    private void setHeight(ImageView ivCard, int height) {
        height = (height < 0) ? 0 : height;
        ViewGroup.LayoutParams params = ivCard.getLayoutParams();
        params.height = height;
        ivCard.setLayoutParams(params);
    }

    private class ImageTouchListener implements View.OnTouchListener {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            int index = event.getActionIndex();
            int action = event.getActionMasked();
            int pointerId = event.getPointerId(index);

            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    if (mVelocityTracker == null) {
                        // Retrieve a new VelocityTracker object to watch the velocity of a motion.
                        mVelocityTracker = VelocityTracker.obtain();
                    } else {
                        // Reset the velocity tracker back to its initial state.
                        mVelocityTracker.clear();
                    }
                    // Add a user's movement to the tracker.
                    mVelocityTracker.addMovement(event);
                    break;
                case MotionEvent.ACTION_MOVE:
                    mVelocityTracker.addMovement(event);
                    // When you want to determine the velocity, call
                    // computeCurrentVelocity(). Then call getXVelocity()
                    // and getYVelocity() to retrieve the velocity for each pointer ID.
                    mVelocityTracker.computeCurrentVelocity(100);
                    float deltaHeight = VelocityTrackerCompat.getYVelocity(mVelocityTracker,
                            pointerId);
                    setHeight((ImageView) view, (int) (view.getHeight() + deltaHeight));
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    // Return a VelocityTracker object back to be re-used by others.
                    mVelocityTracker.recycle();
                    break;
            }
            return true;
        }
    }
}

当我向上滑动并增加高度时它工作得很好但是当我试图减小高度时它会留下一条我不想要的长路。它看起来像这样: enter image description here

有趣的是,当我删除图像的背景颜色并且仅使用源但我需要背景并使用另一个imageview来做同样的事情时,它工作正常并不是IMO的一个好选择。我很确定我在这里遗漏了一些非常基本的东西。请帮忙,我已经坚持了很长时间。

0 个答案:

没有答案