最近编辑于2018年4月30日
方式一、读取res/anim文件夹下的XML文件进行activity过场动画
1、
overridePendingTransition(R.anim.fade, R.anim.hold);
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_longAnimTime" />
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromXDelta="0" android:toXDelta="0"
android:duration="@android:integer/config_longAnimTime" />
2、
overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<scale android:fromXScale="2.0" android:toXScale="1.0"
android:fromYScale="2.0" android:toYScale="1.0"
android:pivotX="50%p" android:pivotY="50%p"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:zAdjustment="top">
<scale android:fromXScale="1.0" android:toXScale=".5"
android:fromYScale="1.0" android:toYScale=".5"
android:pivotX="50%p" android:pivotY="50%p"
android:duration="@android:integer/config_mediumAnimTime" />
<alpha android:fromAlpha="1.0" android:toAlpha="0"
android:duration="@android:integer/config_mediumAnimTime"/>
</set>
方式二、使用ActivityOptions
1、
ActivityOptions opts = ActivityOptions.makeCustomAnimation(Animation.this,
R.anim.fade, R.anim.hold);
startActivity(new Intent(Animation.this, AlertDialogSamples.class), opts.toBundle());
ActivityOptions opts = ActivityOptions.makeCustomAnimation(Animation.this,
R.anim.zoom_enter, R.anim.zoom_enter);
startActivity(new Intent(Animation.this, AlertDialogSamples.class), opts.toBundle());
2、
ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(
v, 0, 0, v.getWidth(), v.getHeight());
startActivity(new Intent(Animation.this, AlertDialogSamples.class), opts.toBundle());
3、
v.setDrawingCacheEnabled(true);
v.setPressed(false);
v.refreshDrawableState();
Bitmap bm = v.getDrawingCache();
ActivityOptions opts = ActivityOptions.makeThumbnailScaleUpAnimation(
v, bm, 0, 0);
startActivity(new Intent(Animation.this, AlertDialogSamples.class), opts.toBundle());
v.setDrawingCacheEnabled(false);
三、启示
从第二种的第三个案例可以得到一种截图(截控件)的方法。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//申请WRITE_EXTERNAL_STORAGE权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
100);
}
findViewById(R.id.tv).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setDrawingCacheEnabled(true);
v.setPressed(false);
v.refreshDrawableState();
Bitmap bitmap = v.getDrawingCache();
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/bitmap.png");
if(file.exists()){
file.delete();
}
FileOutputStream out;
try{
file.createNewFile();
out = new FileOutputStream(file);
if(bitmap.compress(Bitmap.CompressFormat.PNG, 90, out))
{
out.flush();
out.close();
}
} catch (Exception e){
e.printStackTrace();
}
v.setDrawingCacheEnabled(false);
}
});
}
}