Graphics>shadow card drag
这里讲解的是一个textview的拖动阴影效果
1.卡片textview的背景shapedrawable是可变的矩形,三角,圆形,圆角矩形,这里要除三角形之外其他的要使用系统的工具类
private final ShapeDrawable mCardBackground = new ShapeDrawable();
private final ArrayList<Shape> mShapes = new ArrayList<Shape>();
mDensity = getResources().getDisplayMetrics().density;
mShapes.add(new RectShape());
mShapes.add(new OvalShape());
float r = 10 * mDensity;
float radii[] = new float[] {r, r, r, r, r, r, r, r};//四个圆角半径
mShapes.add(new RoundRectShape(radii, null, null));
mShapes.add(new TriangleShape());
mCardBackground.getPaint().setColor(Color.WHITE);
mCardBackground.setShape(mShapes.get(0));//shapedrawable设置一个shape
mCard = findViewById(R.id.card);
mCard.setBackground(mCardBackground);//设置卡片的背景shapedrawable
把这四种shape都放在一个集合中。
通过ShapeDrawable设置shape,在设置给组件。
三角shape是自己定义的:如果自己想要定义要给shape的话可以仿照下面的代码去做。
private static class TriangleShape extends Shape {
private final Path mPath = new Path();
@Override
protected void onResize(float width, float height) {
mPath.reset();
mPath.moveTo(0, 0);
mPath.lineTo(width, 0);
mPath.lineTo(width / 2, height);
mPath.lineTo(0, 0);
mPath.close();
}
@Override
public void draw(Canvas canvas, Paint paint) {
canvas.drawPath(mPath, paint);
}
@Override
public void getOutline(Outline outline) {
outline.setConvexPath(mPath);
}
}
阴影效果的变化是一个动画效果在按下的时候执行了一个属性动画:
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = event.getX() - mCard.getTranslationX();
downY = event.getY() - mCard.getTranslationY();
downTime = event.getDownTime();
ObjectAnimator upAnim = ObjectAnimator.ofFloat(mCard, "translationZ",
MAX_Z_DP * mDensity);
upAnim.setDuration(100);
upAnim.setInterpolator(new DecelerateInterpolator());
upAnim.start();
if (mTiltEnabled) {
mDragState.onDown(event.getDownTime(), event.getX(), event.getY());
}
break;
这的view的位移改变使用的是setTranslation做的通过滑动的位移差值
mCard.setTranslationX(event.getX() - downX);
mCard.setTranslationY(event.getY() - downY);