AlphaAnimation 渐变似的的切换图片
package com.demo.download;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
public class AnimationActivity extends AppCompatActivity {
private Boolean blackFlag = false;
private ImageView ivWhite,ivBlack;
private Button btnChangeAlpha,bt_an;
private ConstraintLayout const_layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animation);
ivWhite = findViewById(R.id.ivWhite);
ivBlack = findViewById(R.id.ivBlack);
btnChangeAlpha= findViewById(R.id.btnChangeAlpha);
const_layout=findViewById(R.id.const_layout);
bt_an= findViewById(R.id.bt_an);
Glide.with(this)
.load("https://productimg.xbiao.com/25/600_900/4366916153456432.jpg")
.into(ivWhite);
Glide.with(this)
.load("https://productimg.xbiao.com/25/600_900/16153439277528.jpg")
.into(ivBlack);
bt_an.setOnClickListener(v -> startActivity(new Intent(AnimationActivity.this,ImageActivity.class)));
}
public void btnChange(View view){
// btnChangeAlpha.setEnabled(false);
if(!blackFlag){
blackFlag =true;
}else {
blackFlag =false;
}
doAnim(blackFlag);
}
private void doAnim(Boolean show){
int startAlpha;
int endAlpha;
if (show) {
//显示
startAlpha = (int) 0f;
endAlpha = (int) 1f;
} else {
//隐藏
startAlpha = (int) 1f;
endAlpha = (int) 0f;
}
AlphaAnimation alphaAnimation =new AlphaAnimation(startAlpha,endAlpha);
alphaAnimation.setDuration(3000);
alphaAnimation.setFillAfter(true);
alphaAnimation.setInterpolator(new LinearInterpolator());
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// btnChangeAlpha.setEnabled(true);
if (show) {
ivBlack.setVisibility(View.GONE);
}
// blackFlag = !blackFlag;
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
if (show) {
ivBlack.setVisibility(View.VISIBLE);
}
ivBlack.startAnimation(alphaAnimation);
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFF00"
tools:context=".MainActivity">
<ImageView
android:id="@+id/ivWhite"
android:layout_width="500dp"
android:layout_height="500dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/ivBlack"
android:layout_width="500dp"
android:layout_height="500dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnChangeAlpha"
android:layout_width="match_parent"
android:layout_height="0dp"
android:onClick="btnChange"
android:text="切换"
app:layout_constraintBottom_toBottomOf="parent" />
<Button
android:id="@+id/bt_an"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动画跳转"
app:layout_constraintBottom_toTopOf="@+id/btnChangeAlpha"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
2 ValueAnimator 实现背景颜色的渐变切换
package com.demo.download;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.backup.BackupAgent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.style.BackgroundColorSpan;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.Button;
import android.widget.ImageView;
/****
* 颜色切换
* https://www.jianshu.com/p/53b5dab7ff30
* https://blog.csdn.net/shibin1990_/article/details/51602498?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-4&spm=1001.2101.3001.4242
*/
public class AlphaAnimationActivity extends AppCompatActivity {
private ImageView mIvImg;
private Button btn;
private AlphaAnimation alphaAnimation;
private ConstraintLayout const_layout;
private ValueAnimator valueAnimator;
private GradienteView gradienteView;
private Boolean isClick = false;
private ValueAnimator mValueAnimator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alpha_animation);
mIvImg = findViewById(R.id.mIvImg);
btn = findViewById(R.id.btn);
const_layout = findViewById(R.id.const_layout);
gradienteView = findViewById(R.id.gv_bg);
valueAnimator = ValueAnimator.ofFloat(0, 1);
valueAnimator.setDuration(2000);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
//可以根据进度百分比 animation.getAnimatedFraction()更新
ArgbEvaluator argbEvaluator = new ArgbEvaluator();
int evaluate = (int) argbEvaluator.evaluate(animation.getAnimatedFraction(), Color.parseColor("#FF000000"), Color.parseColor("#FFFFFFFF"));
const_layout.setBackgroundColor(evaluate);
mIvImg.setBackgroundResource(R.mipmap.a);
}
});
mValueAnimator = ValueAnimator.ofFloat(0, 1);
mValueAnimator.setDuration(2000);
mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ArgbEvaluator argbEvaluator = new ArgbEvaluator();
int evaluate = (int) argbEvaluator.evaluate(animation.getAnimatedFraction(), Color.parseColor("#FFFFFFFF"), Color.parseColor("#FF000000"));
const_layout.setBackgroundColor(evaluate);
mIvImg.setBackgroundResource(R.mipmap.b);
}
});
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isClick) {
isClick = true;
valueAnimator.start();
} else {
isClick = false;
mValueAnimator.start();
}
}
});
findViewById(R.id.tv_start).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gradienteView.init(3000, Color.parseColor("#FF000000"), Color.parseColor("#FFFFFFFF"), new GradienteView.OnColorChangeListener() {
@Override
public void onChange(int color) {
gradienteView.setBackgroundResource(R.mipmap.ic_launcher);
}
});
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mValueAnimator != null || valueAnimator != null) {
mValueAnimator = null;
valueAnimator = null;
}
}
// public void startAlphaAnimation() {
// /**
// * @param fromAlpha 开始的透明度,取值是0.0f~1.0f,0.0f表示完全透明, 1.0f表示和原来一样
// * @param toAlpha 结束的透明度,同上
// */
// alphaAnimation = new AlphaAnimation(1.0f, 0f);
// //设置动画持续时长
// alphaAnimation.setDuration(2000);
// //设置动画结束之后的状态是否是动画的最终状态,true,表示是保持动画结束时的最终状态
// alphaAnimation.setFillAfter(true);
// //设置动画结束之后的状态是否是动画开始时的状态,true,表示是保持动画开始时的状态
// alphaAnimation.setFillBefore(true);
// //设置动画的重复模式:反转REVERSE和重新开始RESTART
// alphaAnimation.setRepeatMode(AlphaAnimation.REVERSE);
// //设置动画播放次数
// alphaAnimation.setRepeatCount(AlphaAnimation.INFINITE);
//
// alphaAnimation.setBackgroundColor(AlphaAnimation.ABSOLUTE);
// //开始动画
// mIvImg.startAnimation(alphaAnimation);
// }
//
// public void cancelAlphaAnimation() {
// //清除动画
// mIvImg.clearAnimation();
// //同样cancel()也能取消掉动画
// alphaAnimation.cancel();
// }
}
public class GradienteView extends View {
public GradienteView(Context context) {
super(context);
}
public GradienteView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
private int drawColor;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(drawColor);
}
//传开始颜色与结束颜色
public void init(int duration, int startColor0x, int endColor0x, OnColorChangeListener listener) {
ValueAnimator animator = ValueAnimator.ofObject(new ArgbEvaluator(), startColor0x, endColor0x);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
drawColor = (Integer) animator.getAnimatedValue();
if (listener != null){
listener.onChange(drawColor);
}
invalidate();
}
});
animator.setDuration(duration);
animator.start();
}
//传开始颜色、中间颜色、结束颜色 多参数可以传很多
public void init(int duration, int startColor0x, int middleColor, int endColor0x, OnColorChangeListener listener) {
ValueAnimator animator = ValueAnimator.ofObject(new ArgbEvaluator(), startColor0x, middleColor, endColor0x);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
drawColor = (Integer) animator.getAnimatedValue();
if (listener != null){
listener.onChange(drawColor);
}
invalidate();
}
});
animator.setDuration(duration);
animator.start();
}
public interface OnColorChangeListener {
void onChange(int color);
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/const_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".AlphaAnimationActivity">
<ImageView
android:id="@+id/mIvImg"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@mipmap/a"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="AlphaAnimation"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mIvImg"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="开始"
android:textSize="18dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mIvImg" />
<com.demo.download.GradienteView
android:id="@+id/gv_bg"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_marginBottom="20dp"
android:background="@mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
3 TransitionDrawable 实现背景图片的渐变切换
public class ImageActivity extends AppCompatActivity {
private Drawable bg_a;
private Drawable bg_b;
LinearLayout up;
int isWhat = 1;
private TextView tv;
private ImageView iv;
private LinearLayout activity_main;
private Button btn_back_ground;
private TransitionDrawable mTransitionDrawable;
private TransitionDrawable imageTransitionDrawable;
private String s = "https://productimg.xbiao.com/25/600_900/3249716153456297.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
up = findViewById(R.id.up);
up.setBackgroundResource(R.mipmap.a);
bg_a = getResources().getDrawable(R.mipmap.a);
bg_b = getResources().getDrawable(R.mipmap.b);
//初始颜色
up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isWhat == 1)
aTob();
else if (isWhat == 2)
bToa();
}
});
initView();
}
private void initView() {
activity_main = findViewById(R.id.activity_main);
// TransitionDrawable mTransitionDrawable = (TransitionDrawable) getResources().getDrawable(R.drawable.activity_main_drawable, null);
// activity_main.setBackground(mTransitionDrawable);
// mTransitionDrawable.startTransition(3000);
iv = findViewById(R.id.iv);
tv = findViewById(R.id.tv);
TransitionDrawable transitionDrawable = (TransitionDrawable) ContextCompat.getDrawable(this, R.drawable.transition_drawable);
tv.setBackground(transitionDrawable);
transitionDrawable.startTransition(3000);
// TransitionDrawable imageTransitionDrawable = new TransitionDrawable(new Drawable[]{ContextCompat.getDrawable(this, R.mipmap.a),
// ContextCompat.getDrawable(this, R.mipmap.b)});
// iv = (ImageView) findViewById(R.id.iv);
// iv.setImageDrawable(imageTransitionDrawable);
// imageTransitionDrawable.startTransition(3000);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (iv.getDrawable() instanceof TransitionDrawable) {
TransitionDrawable transitionDrawable = (TransitionDrawable) iv.getDrawable();
final Drawable first = transitionDrawable.getDrawable(1);
TransitionDrawable btnTransitionDrawable = new TransitionDrawable(new Drawable[]{first, getResources().getDrawable(R.mipmap.a)});
iv.setImageDrawable(btnTransitionDrawable);
btnTransitionDrawable.startTransition(3000);
} else {
final Drawable first = iv.getDrawable();
TransitionDrawable btnTransitionDrawable = new TransitionDrawable(new Drawable[]{first, getResources().getDrawable(R.mipmap.b)});
iv.setImageDrawable(btnTransitionDrawable);
btnTransitionDrawable.startTransition(3000);
}
}
});
btn_back_ground = findViewById(R.id.btn_back_ground);
btn_back_ground.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isWhat == 1) {
backGroundAndImgWhile();
} else {
backGroundAndImgBlack();
}
}
});
}
private void backGroundAndImgWhile() {
mTransitionDrawable = (TransitionDrawable) ContextCompat.getDrawable(this, R.drawable.whilte_drawable);
activity_main.setBackground(mTransitionDrawable);
mTransitionDrawable.startTransition(3000);
imageTransitionDrawable = new TransitionDrawable(new Drawable[]{ContextCompat.getDrawable(this, R.mipmap.a),
ContextCompat.getDrawable(this, R.mipmap.b)});
iv.setImageDrawable(imageTransitionDrawable);
imageTransitionDrawable.startTransition(4000);
isWhat = 2;
}
private void backGroundAndImgBlack() {
mTransitionDrawable = (TransitionDrawable) ContextCompat.getDrawable(this, R.drawable.black_drawable);
activity_main.setBackground(mTransitionDrawable);
mTransitionDrawable.startTransition(3000);
imageTransitionDrawable = new TransitionDrawable(new Drawable[]{ContextCompat.getDrawable(this, R.mipmap.b),
ContextCompat.getDrawable(this, R.mipmap.a)});
iv.setImageDrawable(imageTransitionDrawable);
imageTransitionDrawable.startTransition(4000);
isWhat = 1;
}
private void aTob() {
up.setBackgroundResource(R.mipmap.a);
//渐变切换
TransitionDrawable td = new TransitionDrawable(new Drawable[]{bg_a, bg_b});
up.setBackgroundDrawable(td);
td.startTransition(1000);
isWhat = 2;
}
private void bToa() {
up.setBackgroundResource(R.mipmap.b);
//渐变切换
TransitionDrawable td = new TransitionDrawable(new Drawable[]{bg_b, bg_a});
up.setBackgroundDrawable(td);
td.startTransition(1000);
isWhat = 1;
}
}
transition_drawable
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/teal_200" />
<item android:drawable="@color/black" />
</transition>
//whilte_drawable
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/white" />
<item android:drawable="@color/black" />
</transition>
//black_drawable
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/black" />
<item android:drawable="@color/white" />
</transition>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/up"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginStart="50dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="50dp"
android:background="#3F51B5"
android:orientation="horizontal" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:gravity="center_horizontal|center_vertical"
android:text="Hello World!" />
<ImageView
android:id="@+id/iv"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="40dp"
android:contentDescription="null" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical|center_horizontal"
android:text="图片" />
<Button
android:id="@+id/btn_back_ground"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical|center_horizontal"
android:text="切换背景" />
</LinearLayout>