首页 > 代码库 > 如果模拟一种图标被删除时一种颜色渐变的效果,采用TransitionDrawable实现
如果模拟一种图标被删除时一种颜色渐变的效果,采用TransitionDrawable实现
效果图:
TransitionDrawable 可以用来实现两个Drawable直接的交错渐变的过渡效果
接着上篇,我们这样来实现:
public class MyDragLayer extends FrameLayout { /** * The bitmap that is currently being dragged */ private Bitmap mDragBitmap = null; private float mLastMotionX; private float mLastMotionY; private float mOffsetX; private float mOffsetY; private static final int TRANSITION_DURATION = 250; public View mTrashBin; public View mDropTarget; private final Paint mTrashPaint = new Paint(); private Paint mDragPaint; private TransitionDrawable mTransition; public MyDragLayer(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public MyDragLayer(Context context, AttributeSet attrs) { super(context, attrs); } public MyDragLayer(Context context) { super(context); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { mLastMotionX = ev.getX(); mLastMotionY = ev.getY(); int dx = 0; int dy = 0; if (mDragBitmap != null) { dx = (int)(mLastMotionX - mOffsetX + mDragBitmap.getWidth() / 2); dy = (int)(mLastMotionY - mOffsetY + mDragBitmap.getHeight() / 2); } int action = ev.getAction(); if (action == MotionEvent.ACTION_MOVE) { //如果之前进入trash view的范围,则mDropTarget必须是trashbin boolean preIsTrash = mDropTarget == mTrashBin; mDropTarget = findDropTarget(dx, dy); boolean currentIsTrash = mDropTarget == mTrashBin; if (!preIsTrash && currentIsTrash) { // 由外进入垃圾箱 mTransition.reverseTransition(TRANSITION_DURATION); } else if (preIsTrash && !currentIsTrash) { // 有垃圾箱往外 mTransition.reverseTransition(TRANSITION_DURATION); } if (currentIsTrash) { mDragPaint = mTrashPaint; } else { mDragPaint = null; } } else if (action == MotionEvent.ACTION_UP) { mDropTarget = findDropTarget(dx, dy); if (mDropTarget == mTrashBin) { invalidate(); return true; //QuickNavGridView will receive Action_cancel } } invalidate(); boolean result = super.onInterceptTouchEvent(ev); return result; } private View findDropTarget(int x, int y) { if (mTrashBin != null && mTrashBin.getVisibility() == View.VISIBLE) { Rect r = new Rect(); //将mTrashBin的坐标映射到Rect r上 mTrashBin.getHitRect(r); if (r.contains(x, y)) { return mTrashBin; } } return null; } @Override protected void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); if (mDragBitmap != null && !mDragBitmap.isRecycled()) { // Draw actual icon being dragged canvas.drawBitmap(mDragBitmap, getScrollX() + mLastMotionX - mOffsetX, getScrollY() + mLastMotionY - mOffsetY, mDragPaint); } } public void startDrag(Bitmap bitmap, int offsetx, int offsety) { mDragBitmap = bitmap; mOffsetX = offsetx; mOffsetY = offsety; mDragPaint = null; invalidate(); } public void setTrashBin(View view) { mTrashBin = view; mTransition = (TransitionDrawable) view.getBackground(); } }
activity:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test); MyLinearlayout layout = (MyLinearlayout)findViewById(R.id.linear); layout.mDragLayer = (MyDragLayer) findViewById(R.id.rootView); layout.mDragLayer.setTrashBin(findViewById(R.id.trashbin)); }
代码:http://download.csdn.net/detail/baidu_nod/7759965
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。