项目效果:
![](https://i-blog.csdnimg.cn/blog_migrate/7d9c36f9145677ce10b64c6a2a577116.gif)
项目结构:
ListAdapter.java
package com.waterflow.myanimationtest1;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ListAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private List<String> names = new ArrayList<>();
public ListAdapter(Context context){
inflater = LayoutInflater.from(context);
names.add("山峰");
names.add("雪山");
names.add("海滩");
names.add("绿叶");
}
@Override
public int getCount() {
return names.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Holder holder;
if(view == null){
holder = new Holder();
view = inflater.inflate(R.layout.listitem_img,null);
holder.name = view.findViewById(R.id.name);
view.setTag(holder);
}else {
holder = (Holder) view.getTag();
}
holder.name.setText(names.get(i));
return view;
}
class Holder{
TextView name;
}
}
ViewWrapper.java
package com.waterflow.myanimationtest1;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
public class ViewWrapper {
private View view;
private LinearLayout.LayoutParams params;
private int originwidth,originheight;
public ViewWrapper(View view){
this.view = view;
params = (LinearLayout.LayoutParams) view.getLayoutParams();
originwidth = view.getContext().getResources().getDisplayMetrics().widthPixels;
originheight = view.getContext().getResources().getDisplayMetrics().heightPixels;
Log.d("view-"+view.getId(),originwidth+"x"+originheight);
}
int getWidth(){
return params.width < 0 ? originwidth : params.width;
}
int getHeight(){
return params.height < 0 ? originheight : params.height;
}
void setWidth(float width){
if(width == originwidth){
params.width = -1;
params.setMargins(0,0,0,0);
}else{
params.width = (int)width;
}
view.setLayoutParams(params);
}
void setHeight(float height){
params.height = (int) height;
view.setLayoutParams(params);
}
int getOriginwidth(){
return originwidth;
}
int getOriginheight(){
return originheight;
}
void setMarginRight(float right){
params.setMargins(0,0,(int)right,0);
view.setLayoutParams(params);
}
int getMarginRight(){
return params.rightMargin;
}
}
MainActivity.java
package com.waterflow.myanimationtest1;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private LinearLayout linear1;
private ImageView iv1;
private LinearLayout linear2;
private ListView list_img;
int pressx = 0,pressy = 0 ;
ViewWrapper wraplinear1,wrapiv,wraplinear2;
private TextView tvTitle;
private TextView tvDesc;
private Button btnBefore;
private Button btnNext;
int count = 0;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linear1 = (LinearLayout) findViewById(R.id.linear1);
iv1 = (ImageView) findViewById(R.id.iv1);
linear2 = (LinearLayout) findViewById(R.id.linear2);
list_img = (ListView) findViewById(R.id.list_imgs);
tvTitle = (TextView) findViewById(R.id.tv_title);
tvDesc = (TextView) findViewById(R.id.tv_desc);
btnBefore = (Button) findViewById(R.id.btn_before);
btnNext = (Button) findViewById(R.id.btn_next);
setContent(0);
ListAdapter adapter = new ListAdapter(this);
list_img.setAdapter(adapter);
list_img.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
count = i;
setContent(i);
if(iv1.getAlpha() == 0){
AnimatorSet set = new AnimatorSet();
//否则复原
ObjectAnimator width = ObjectAnimator.ofFloat(wrapiv,"width",wrapiv.getWidth(),wrapiv.getOriginwidth());
ObjectAnimator height1 = ObjectAnimator.ofFloat(wrapiv,"height",wrapiv.getHeight(),wrapiv.getOriginwidth()*9/16);
ObjectAnimator height2 = ObjectAnimator.ofFloat(wraplinear2,"height",wraplinear2.getHeight(),wraplinear2.getOriginheight()-wrapiv.getOriginwidth()*9/16);
ObjectAnimator height3 = ObjectAnimator.ofFloat(wraplinear1,"height",wraplinear1.getHeight(),wraplinear1.getOriginheight());
ObjectAnimator alpha = ObjectAnimator.ofFloat(linear2,"alpha",linear2.getAlpha(),1);
ObjectAnimator margin = ObjectAnimator.ofFloat(wrapiv,"marginRight",(wrapiv.getOriginwidth()-wrapiv.getWidth())*100/wrapiv.getOriginwidth(),0);
ObjectAnimator alpha2 = ObjectAnimator.ofFloat(iv1,"alpha",0,1);
set.playTogether(width,height1,height2,height3,alpha,margin,alpha2);
set.setDuration(1000);
set.start();
}
}
});
btnBefore.setOnClickListener(this);
btnNext.setOnClickListener(this);
wraplinear1 = new ViewWrapper(linear1);
wrapiv = new ViewWrapper(iv1);
wraplinear2 = new ViewWrapper(linear2);
iv1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
pressx = (int) motionEvent.getX();
pressy = (int) motionEvent.getY();
break;
case MotionEvent.ACTION_MOVE:
int distancey = (int) (motionEvent.getY() - pressy);
int distancex = (int) (motionEvent.getX() - pressx);
if(Math.abs(distancey)>200){
wraplinear1.setHeight(wraplinear1.getHeight() - distancey / 5);
if (wrapiv.getWidth() - distancey/10 < 600){
wrapiv.setWidth(600);
}else if(wrapiv.getWidth() - distancey/10 > wrapiv.getOriginwidth()){
wrapiv.setWidth(wrapiv.getOriginwidth());
}else {
wrapiv.setWidth(wrapiv.getWidth() - distancey/10);
}
wrapiv.setHeight(wrapiv.getWidth()*9/16);
wrapiv.setMarginRight((wrapiv.getOriginwidth()-wrapiv.getWidth())*100/wrapiv.getOriginwidth());
}
if(Math.abs(distancex) > 100 && linear2.getAlpha() == 0){
wrapiv.setMarginRight(-distancex);
}
break;
case MotionEvent.ACTION_UP:
AnimatorSet set = new AnimatorSet();
if(wrapiv.getWidth() == 600){
//变成最小化形态
ObjectAnimator height = ObjectAnimator.ofFloat(wraplinear1,"height",wraplinear1.getHeight(),wrapiv.getHeight()+50);
ObjectAnimator alpha = ObjectAnimator.ofFloat(linear2,"alpha",linear2.getAlpha(),0);
set.playTogether(height,alpha);
}else {
//否则复原
ObjectAnimator width = ObjectAnimator.ofFloat(wrapiv,"width",wrapiv.getWidth(),wrapiv.getOriginwidth());
ObjectAnimator height1 = ObjectAnimator.ofFloat(wrapiv,"height",wrapiv.getHeight(),wrapiv.getOriginwidth()*9/16);
ObjectAnimator height2 = ObjectAnimator.ofFloat(wraplinear2,"height",wraplinear2.getHeight(),wraplinear2.getOriginheight()-wrapiv.getOriginwidth()*9/16);
ObjectAnimator height3 = ObjectAnimator.ofFloat(wraplinear1,"height",wraplinear1.getHeight(),wraplinear1.getOriginheight());
ObjectAnimator alpha = ObjectAnimator.ofFloat(linear2,"alpha",linear2.getAlpha(),1);
ObjectAnimator margin = ObjectAnimator.ofFloat(wrapiv,"marginRight",(wrapiv.getOriginwidth()-wrapiv.getWidth())*100/wrapiv.getOriginwidth(),0);
set.playTogether(width,height1,height2,height3,alpha,margin);
}
set.setDuration(1000);
set.start();
if(wrapiv.getWidth() == 600){
AnimatorSet set2 = new AnimatorSet();
Log.d("value","rightMargin:"+wrapiv.getMarginRight()+","+(wrapiv.getOriginwidth()/2));
if(wrapiv.getMarginRight() > wrapiv.getOriginwidth()/3){
ObjectAnimator alpha = ObjectAnimator.ofFloat(iv1,"alpha",1,0);
ObjectAnimator margin = ObjectAnimator.ofFloat(wrapiv,"marginRight",wrapiv.getMarginRight(),wrapiv.getOriginwidth());
set2.playTogether(alpha,margin);
}else{
ObjectAnimator margin = ObjectAnimator.ofFloat(wrapiv,"marginRight",wrapiv.getMarginRight(),(wrapiv.getOriginwidth()-wrapiv.getWidth())*100/wrapiv.getOriginwidth());
set2.playTogether(margin);
}
set2.setDuration(1000);
set2.start();
}
break;
default:
break;
}
return true;
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_before:
count = (count-1)%4;
if(count < 0){
count = 3;
}
setContent(count);
break;
case R.id.btn_next:
count = (count+1)%4;
setContent(count);
break;
}
}
void setContent(int i){
int[] pics = {R.drawable.mountain,R.drawable.snowmountain,R.drawable.beach,R.drawable.leaf};
String[] titles = {"山峰","雪山","海滩","绿叶"};
String[] descs = {
"日头将落下那一边天空,还剩有无数云彩,这些云彩阻拦了日头,却为日头的光烘出炫目美丽的颜色。这一边,有一些云彩镶了金边、白边、玛瑙边、淡紫边,如都市中妇人的衣缘,精致而又华丽。云彩无色不备,在空中以一种魔术师的手法,不断的在流动变化。空气因为雨后而澄清,一切景色皆如一人久病新瘥的神气。",
"玉龙雪山非常高,最高点是3600米高,但是我看不到山顶,因为云把山分成两部分,而山顶是上部,被云抓住。所以我决定坐缆车上山,看看这座3600米高的雪山有多壮观。",
"站在海滩上,海水像母亲的手,抚摸着你,让你感受到深沉的母爱,海水又是像一只温顺的小狗,安安静静,倚在你的身旁,但有时又有“东临碣石,以观沧海”的叱咤风云。",
"春天里有一种树叶,它由三片叶子构成,翠绿翠绿的,而且都是椭圆形,有一点长,如果合在一起就像“千手观音”,漂亮极了!如果将它中间一片叶子摘下来它就像英文字母“Y”,也像“yes”的手势,非常好玩。"
};
iv1.setImageResource(pics[i]);
tvTitle.setText(titles[i]);
tvDesc.setText(descs[i]);
}
}
图片文件在此: