国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android圖片翻轉動畫簡易實現代碼

2020-04-11 12:40:53
字體:
來源:轉載
供稿:網友

下面給大家分享一個有趣的動畫:這里比較適合一張圖片的翻轉,如果是多張圖片,可以參考APIDemo里的例子,就是加個ArrayAdapter,還是簡單的,也可以自己發揮修改,實現自己想要的。這里的代碼基本上可以直接運行項目了。
在main.xml里加個ImageView,如

復制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate"
android:textSize="50px"
android:layout_x="150px"
android:layout_y="30px"
android:src="@drawable/ro">
></ImageView>
</FrameLayout>

這個不需要解釋吧,都可以看懂的
最后,還需要一個activity類
如:
復制代碼 代碼如下:

public class TestRotate extends Activity implements OnClickListener{
private mageView imageview;
private ViewGroup mContainer;
/**
*這個變量設置的是圖片,如果是多張圖片,那么可以用數組,如
*private static final int IMAGE = new int[]{
* R.drawable.ro,
* R.drawable.icon
*};
*有多少圖片就放多少,我這里做的只是一張圖片的翻轉
*
*/
private static final int IMAGE = R.drawable.ro;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageview = (ImageView) findViewById(R.id.image);
mContainer = (ViewGroup) findViewById(R.id.container);
/**
* 設置最新顯示的圖片
* 如果是數組,那么可以寫成IMAGE[int]
*
*/
imageview.setImageResource(IMAGE);
/**
*
* 設置ImageView的OnClickListener
*
*/
imageview.setClickable(true);
imageview.setFocusable(true);
imageview.setOnClickListener(this);
}
private void applyRotation(int position, float start, float end) {
// Find the center of the container
final float centerX = mContainer.getWidth() / 2.0f;
final float centerY = mContainer.getHeight() / 2.0f;
final Rotate3d rotation =
new Rotate3d(start, end, centerX, centerY, 310.0f, true);
rotation.setDuration(500);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new DisplayNextView(position));
mContainer.startAnimation(rotation);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/**
*
* 調用這個方法,就是翻轉圖片
* 參數很簡單,大家都應該看得懂
* 簡單說下,第一個是位置,第二是開始的角度,第三個是結束的角度
* 這里需要說明的是,如果是要回到上一張
* 把第一個參數設置成-1就行了
*
*/
applyRotation(0,0,90);
}
private final class DisplayNextView implements Animation.AnimationListener {
private final int mPosition;
private DisplayNextView(int position) {
mPosition = position;
}
public void onAnimationStart(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
mContainer.post(new SwapViews(mPosition));
}
public void onAnimationRepeat(Animation animation) {
}
}
/**
* This class is responsible for swapping the views and start the second
* half of the animation.
*/
private final class SwapViews implements Runnable {
private final int mPosition;
public SwapViews(int position) {
mPosition = position;
}
public void run() {
final float centerX = mContainer.getWidth() / 2.0f;
final float centerY = mContainer.getHeight() / 2.0f;
Rotate3d rotation;
if (mPosition > -1) {
imageview.setVisibility(View.VISIBLE);
imageview.requestFocus();
rotation = new Rotate3d(90, 180, centerX, centerY, 310.0f, false);
} else {
imageview.setVisibility(View.GONE);
rotation = new Rotate3d(90, 0, centerX, centerY, 310.0f, false);
}
rotation.setDuration(500);
rotation.setFillAfter(true);
rotation.setInterpolator(new DecelerateInterpolator());
mContainer.startAnimation(rotation);
}
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 湾仔区| 滦南县| 鄢陵县| 岳普湖县| 镇江市| 鸡东县| 双峰县| 新乡市| 桂平市| 绥江县| 彭阳县| 铜川市| 扶风县| 丁青县| 香格里拉县| 兴城市| 永济市| 丰镇市| 宁强县| 韶山市| 阳高县| 龙陵县| 河西区| 武定县| 柏乡县| 瓮安县| 武穴市| 左云县| 桃园县| 桦川县| 松原市| 招远市| 湖北省| 含山县| 耒阳市| 闸北区| 大荔县| 衢州市| 谷城县| 图木舒克市| 崇州市|