-
Notifications
You must be signed in to change notification settings - Fork 2
/
Practice01Translation.java
83 lines (68 loc) · 2.53 KB
/
Practice01Translation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.geekholt.practiceui.view.lesson5;
import android.content.Context;
import android.graphics.Outline;
import android.graphics.Path;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import com.geekholt.practiceui.R;
import static android.os.Build.VERSION.SDK_INT;
import static com.geekholt.practiceui.util.ScreenUtil.dpToPixel;
public class Practice01Translation extends RelativeLayout {
Button animateBt;
ImageView imageView;
public Practice01Translation(Context context) {
super(context);
}
public Practice01Translation(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public Practice01Translation(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
animateBt = (Button) findViewById(R.id.animateBt);
imageView = (ImageView) findViewById(R.id.imageView);
if (SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
// 给音乐图标加上合适的阴影
imageView.setOutlineProvider(new MusicOutlineProvider());
}
animateBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
// TODO 在这里处理点击事件,通过 View.animate().translationX/Y/Z() 来让 View 平移
imageView.animate().translationX(200f)
.translationY(200f)
.translationZ(200f);
}
});
}
/**
* 为音乐图标设置三角形的 Outline。
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
class MusicOutlineProvider extends ViewOutlineProvider {
Path path = new Path();
{
path.moveTo(0, dpToPixel(10));
path.lineTo(dpToPixel(7), dpToPixel(2));
path.lineTo(dpToPixel(116), dpToPixel(58));
path.lineTo(dpToPixel(116), dpToPixel(70));
path.lineTo(dpToPixel(7), dpToPixel(128));
path.lineTo(0, dpToPixel(120));
path.close();
}
@Override
public void getOutline(View view, Outline outline) {
outline.setConvexPath(path);
}
}
}