博主比较懒,所以贴一些关键代码:
代码中写有相应的解释,我这里就不多说了。主要说一下关于我的变亮和变暗的思路:
设置底下那层activity的背景颜色为黑色,并且主题为可透明(后面会有给出),然后通过改变底下那层layout的alpha,也就是透明度,从而达到逐明逐暗的效果。
(由于录制gif出现花屏的现象,使用自己的另一个图来代替,效果相同。)
下面上代码:
java文件:
main_activity.java:
package com.example.test_csdn;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
public class main_activity extends Activity implements View.OnClickListener{
private Button button;
public static LinearLayout MainLayout; //记住这里设为静态变量后
private Interpolator interpolator;//这是一个插值器,目的是让某个值随时间均匀变化
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button) findViewById(R.id.main_button);
button.setOnClickListener(this);
MainLayout=(LinearLayout) findViewById(R.id.main_layout);
interpolator=new LinearInterpolator(); //LinearInterpolator表示匀速变化
}
//这是点击事件
@Override
public void onClick(View v) {
System.out.println("你好");
switch (v.getId()){
case R.id.main_button:
System.out.println("你好");
Intent intent_personal=new Intent(main_activity.this,other_activity.class);
startActivity(intent_personal);
overridePendingTransition(R.anim.base_slide_left_in,R.anim.base_slide_remian);
/**
* 此处的overridePendingTransition(R.anim.a,R.anim.b)方法是实现一个activity跳转到另一个activity的切换动画
* 其中,a是指即将出现的activity的出现方式,b是指即将消失的activity的消失方式
*
* */
backgroundAlpha();
}
}
/*
backgroundAlpha()方法是实现页面跳转时下一层逐渐变暗
由于添加了匀速的插值器interpolator,所以这里的ofFloat(a,b)是指animator的值由a匀速变成b,在500毫秒之内
*/
private void backgroundAlpha(){
ValueAnimator animator=ValueAnimator.ofFloat(1f,0.2f);
animator.setDuration(800);
animator.setInterpolator(interpolator);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
/*
此处setAlpha()方法是设置MainLayout的透明度
注意:此时需要将该activity的主题的背景颜色设置为黑色,这样才会有那种逐渐变黑的效果,
如果设为白色,则会是逐渐变成透明
*/
MainLayout.setAlpha((Float) (animation.getAnimatedValue()));
}
});
animator.start();
}
}
other_activity.java:
package com.example.test_csdn;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.Nullable;
public class other_activity extends Activity {
private View decorView; //表示根视图
private float downX,downY; //表示刚刚按下时的x与y坐标
private float screenWidth; //表示屏幕的长与宽
private DisplayMetrics metrics; //获取手机的
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
//获取屏幕的宽
metrics=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth=metrics.widthPixels;
//获取屏幕的decorView
decorView=getWindow().getDecorView();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Float moveDistenceX;
switch (event.getAction()){
case MotionEvent.ACTION_DOWN: //手指按下屏幕时触发
downX=event.getX();
break;
case MotionEvent.ACTION_MOVE: //手指在屏幕上移动时触发
moveDistenceX=event.getX()-downX;
if (moveDistenceX<0){ //小于0则表示是向左滑动
decorView.setX(moveDistenceX); //设置界面的X到滑动到的位置,这里时设置界面的x=0的位置
main_activity.MainLayout.setAlpha(((Float) moveDistenceX/(-screenWidth)));//注意这里是改变MainLayout的
}
break;
case MotionEvent.ACTION_UP: //手指抬起时触发
moveDistenceX=event.getX()-downX;
if (moveDistenceX<-200){ //如果滑动的距离超过200,就结束当前的activity
continueMove(moveDistenceX);
} else { //如果没有超过一半就恢复到初始的状态
rebackToLeft(moveDistenceX);
}
break;
}
return super.onTouchEvent(event);
}
//手松开后继续滑动的动画
public void continueMove(float moveDistenceX){
ValueAnimator animator=ValueAnimator.ofFloat(moveDistenceX,-screenWidth);
animator.setDuration(1000); //表示0.2s的时间来结束后面的动画
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float x=(float)(animation.getAnimatedValue());
decorView.setX(x); //结束动画实现在这里
main_activity.MainLayout.setAlpha(((Float) (animation.getAnimatedValue())/(-screenWidth)));
}
});
animator.addListener(new AnimatorListenerAdapter() {
@Override
//ValueAnimator的四种方法之一,表示在动画结束时促发的事件
public void onAnimationEnd(Animator animation) {
finish();
}
});
animator.start();
}
//滑动没有超过200,就滑回去
private void rebackToLeft(float moveDistanceX){
ObjectAnimator.ofFloat(decorView, "X", moveDistanceX, 0).setDuration(300).start();
}
}
layout文件:
mian.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center">
<Button
android:id="@+id/main_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="点击进入下一个actvity"/>
</LinearLayout>
other.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/other_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textColor="#000000"
android:textStyle="bold"
android:text="武汉加油"/>
</LinearLayout>
anim文件:
base_slide_left_in.xml :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="800"
android:fromXDelta="-100.0%"
android:toXDelta="0.0%"
android:interpolator="@android:anim/accelerate_interpolator"/>
</set>
base_slide_right_out.xml :
```java
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="800"
android:fromXDelta="0%"
android:toXDelta="-100.0%"
android:interpolator="@android:anim/accelerate_interpolator"/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0"
android:duration="1000"></alpha>
</set>
base_slide_remian.xml :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="800"
android:fromXDelta="0%"
android:toXDelta="0%" />
<alpha
android:fromAlpha="0"
android:toAlpha="0"
android:duration="1000"></alpha>
</set>
manifests文件:
AndroidManifest.xml :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test_csdn">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".main_activity"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".other_activity"
android:theme="@style/MyTheme">
</activity>
</application>
</manifest>
emmmm~,如果由更好的方式或是博主有不足的地方望告知,有些细节部分我想不到了,如果对发现的小可爱有用的话,那最好了!