我是靠谱客的博主 自由小鸭子,这篇文章主要介绍Android点击查看大图过渡动画与图片缩放与移动,现在分享给大家,希望可以做个参考。

从一个activity到另一个activity的过渡

1.小图点击事件代码

复制代码
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
@Override public void onClick(View view) { switch (view.getId()) { case R.id.img_1: view.setClickable(false); Intent intentS = new Intent(this, ImageActivity.class); int[] screenLocationS = new int[2]; view.getLocationOnScreen(screenLocationS); intentS.putExtra(LEFT, screenLocationS[0]).//将图片位置传到大图activity用于动画初始位置 putExtra(TOP, screenLocationS[1]). putExtra(WIDTH, view.getWidth()). putExtra(HEIGHT, view.getHeight()). putExtra(IMAGE, _snapUrl). putExtra(TITLE, _name); startActivity(intentS); overridePendingTransition(0, 0);//取消原有默认的Activity到Activity的过渡动画 break; case R.id.img_2: view.setClickable(false); Intent intentM = new Intent(this, ImageActivity.class); int[] screenLocationM = new int[2]; view.getLocationOnScreen(screenLocationM); intentM.putExtra(LEFT, screenLocationM[0]).//将图片位置传到大图activity用于动画初始位置 putExtra(TOP, screenLocationM[1]). putExtra(WIDTH, view.getWidth()). putExtra(HEIGHT, view.getHeight()). putExtra(IMAGE, _matchUrl). putExtra(TITLE, _name); startActivity(intentM); overridePendingTransition(0, 0);//取消原有默认的Activity到Activity的过渡动画 break; } }

2.大图Activity代码

复制代码
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
public class BigImageActivity extends Activity implements View.OnClickListener { private static final int DURATION = 150; public final static String TITLE = "Title"; public final static String TOP = "Top"; public final static String LEFT = "Left"; public final static String WIDTH = "Width"; public final static String HEIGHT = "Height"; public final static String IMAGE = "Image"; private int mLeftDelta; private int mTopDelta; private float mWidthScale; private float mHeightScale; private int intentTop; private int intentLeft; private int intentWidth; private int intentHeight; private LinearLayout linearLayout; private ColorDrawable colorDrawable; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FaceDetectionApp.setWindowTrans(this, true, false); setContentView(R.layout.activity_image); initView(); if (savedInstanceState == null) { ViewTreeObserver observer = imageView.getViewTreeObserver(); observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { imageView.getViewTreeObserver().removeOnPreDrawListener(this); int[] screenLocation = new int[2]; imageView.getLocationOnScreen(screenLocation); mLeftDelta = intentLeft - screenLocation[0]; mTopDelta = intentTop - screenLocation[1]; mWidthScale = (float) intentWidth / imageView.getWidth(); mHeightScale = (float) intentHeight / imageView.getHeight(); enterAnimation(new Runnable() { @Override public void run() { Matrix matrix = imageView.getMatrix(); imageView.setImageMatrix(matrix); imageView.setScaleType(ImageView.ScaleType.MATRIX); imageView.setOnTouchListener(new ImageTouchListener()); } }); return true; } }); } } @Override protected void initView() { super.initView(); linearLayout = findViewById(R.id.ll_img); imageView = findViewById(R.id.img); initValue(); } @Override protected void initValue() { super.initValue(); colorDrawable = new ColorDrawable(ContextCompat.getColor(this, R.color.color_item_background)); linearLayout.setBackground(colorDrawable); Bundle bundle = getIntent().getExtras(); if (bundle != null) { title = bundle.getString(TITLE); intentTop = bundle.getInt(TOP); intentLeft = bundle.getInt(LEFT); intentWidth = bundle.getInt(WIDTH); intentHeight = bundle.getInt(HEIGHT); String imgURL = bundle.getString(IMAGE); asyncLoadImageSmallList(imageView, imgURL);//框架代码 不解释 imageView.setOnClickListener(this); } } @Override public void onClick(View view) { switch (view.getId()) { case R.id.img: view.setClickable(false); exitAnimation(new Runnable() { public void run() { finish(); overridePendingTransition(0, 0); } }); break; } } @Override public void onBackPressed() { exitAnimation(new Runnable() { public void run() { finish(); overridePendingTransition(0, 0); } }); } public void enterAnimation(final Runnable enterAction) { imageView.setPivotX(0); imageView.setPivotY(0); imageView.setScaleX(mWidthScale); imageView.setScaleY(mHeightScale); imageView.setTranslationX(mLeftDelta); imageView.setTranslationY(mTopDelta); TimeInterpolator sDecelerator = new DecelerateInterpolator(); imageView.animate().setDuration(DURATION).scaleX(1).scaleY(1). translationX(0).translationY(0).setInterpolator(sDecelerator).withEndAction(enterAction); ObjectAnimator bgAnim = ObjectAnimator.ofInt(colorDrawable, "alpha", 0, 255); bgAnim.setDuration(DURATION); bgAnim.start(); } public void exitAnimation(final Runnable endAction) { TimeInterpolator sInterpolator = new AccelerateInterpolator(); imageView.animate().setDuration(DURATION).scaleX(mWidthScale).scaleY(mHeightScale). translationX(mLeftDelta).translationY(mTopDelta).setInterpolator(sInterpolator).withEndAction(endAction); ObjectAnimator bgAnim = ObjectAnimator.ofInt(colorDrawable, "alpha", 0); bgAnim.setDuration(DURATION); bgAnim.start(); } }

最后

以上就是自由小鸭子最近收集整理的关于Android点击查看大图过渡动画与图片缩放与移动的全部内容,更多相关Android点击查看大图过渡动画与图片缩放与移动内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(82)

评论列表共有 0 条评论

立即
投稿
返回
顶部