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

首頁 > 系統(tǒng) > Android > 正文

Android實(shí)現(xiàn)中軸旋轉(zhuǎn)特效 Android制作別樣的圖片瀏覽器

2019-10-22 18:21:52
字體:
供稿:網(wǎng)友

Android API Demos中有很多非常Nice的例子,這些例子的代碼都寫的很出色,如果大家把API Demos中的每個(gè)例子研究透了,那么恭喜你已經(jīng)成為一個(gè)真正的Android高手了。這也算是給一些比較迷茫的Android開發(fā)者一個(gè)指出了一個(gè)提升自我能力的方向吧。API Demos中的例子眾多,今天我們就來模仿其中一個(gè)3D變換的特效,來實(shí)現(xiàn)一種別樣的圖片瀏覽器。

既然是做中軸旋轉(zhuǎn)的特效,那么肯定就要用到3D變換的功能。在Android中如果想要實(shí)現(xiàn)3D效果一般有兩種選擇,一是使用Open GL ES,二是使用Camera。Open GL ES使用起來太過復(fù)雜,一般是用于比較高級(jí)的3D特效或游戲,像比較簡單的一些3D效果,使用Camera就足夠了。

Camera中提供了三種旋轉(zhuǎn)方法,分別是rotateX()、rotateY()和rotateZ,調(diào)用這三個(gè)方法,并傳入相應(yīng)的角度,就可以讓視圖圍繞這三個(gè)軸進(jìn)行旋轉(zhuǎn),而今天我們要做的中軸旋轉(zhuǎn)效果其實(shí)就是讓視圖圍繞Y軸進(jìn)行旋轉(zhuǎn)。使用Camera讓視圖進(jìn)行旋轉(zhuǎn)的示意圖,如下所示:

Android中軸旋轉(zhuǎn)特效,Android圖片瀏覽器,Android旋轉(zhuǎn)特效

那我們就開始動(dòng)手吧,首先創(chuàng)建一個(gè)Android項(xiàng)目,起名叫做RotatePicBrowserDemo,然后我們準(zhǔn)備了幾張圖片,用于稍后在圖片瀏覽器中進(jìn)行瀏覽。

而API Demos中已經(jīng)給我們提供了一個(gè)非常好用的3D旋轉(zhuǎn)動(dòng)畫的工具類Rotate3dAnimation,這個(gè)工具類就是使用Camera來實(shí)現(xiàn)的,我們先將這個(gè)這個(gè)類復(fù)制到項(xiàng)目中來,代碼如下所示:

/**  * An animation that rotates the view on the Y axis between two specified angles.  * This animation also adds a translation on the Z axis (depth) to improve the effect.  */ public class Rotate3dAnimation extends Animation {  private final float mFromDegrees;  private final float mToDegrees;  private final float mCenterX;  private final float mCenterY;  private final float mDepthZ;  private final boolean mReverse;  private Camera mCamera;   /**   * Creates a new 3D rotation on the Y axis. The rotation is defined by its   * start angle and its end angle. Both angles are in degrees. The rotation   * is performed around a center point on the 2D space, definied by a pair   * of X and Y coordinates, called centerX and centerY. When the animation   * starts, a translation on the Z axis (depth) is performed. The length   * of the translation can be specified, as well as whether the translation   * should be reversed in time.   *   * @param fromDegrees the start angle of the 3D rotation   * @param toDegrees the end angle of the 3D rotation   * @param centerX the X center of the 3D rotation   * @param centerY the Y center of the 3D rotation   * @param reverse true if the translation should be reversed, false otherwise   */  public Rotate3dAnimation(float fromDegrees, float toDegrees,    float centerX, float centerY, float depthZ, boolean reverse) {   mFromDegrees = fromDegrees;   mToDegrees = toDegrees;   mCenterX = centerX;   mCenterY = centerY;   mDepthZ = depthZ;   mReverse = reverse;  }   @Override  public void initialize(int width, int height, int parentWidth, int parentHeight) {   super.initialize(width, height, parentWidth, parentHeight);   mCamera = new Camera();  }   @Override  protected void applyTransformation(float interpolatedTime, Transformation t) {   final float fromDegrees = mFromDegrees;   float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);    final float centerX = mCenterX;   final float centerY = mCenterY;   final Camera camera = mCamera;    final Matrix matrix = t.getMatrix();    camera.save();   if (mReverse) {    camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);   } else {    camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));   }   camera.rotateY(degrees);   camera.getMatrix(matrix);   camera.restore();    matrix.preTranslate(-centerX, -centerY);   matrix.postTranslate(centerX, centerY);  } } 

可以看到,這個(gè)類的構(gòu)造函數(shù)中接收一些3D旋轉(zhuǎn)時(shí)所需用到的參數(shù),比如旋轉(zhuǎn)開始和結(jié)束的角度,旋轉(zhuǎn)的中心點(diǎn)等。然后重點(diǎn)看下applyTransformation()方法,首先根據(jù)動(dòng)畫播放的時(shí)間來計(jì)算出當(dāng)前旋轉(zhuǎn)的角度,然后讓Camera也根據(jù)動(dòng)畫播放的時(shí)間在Z軸進(jìn)行一定的偏移,使視圖有遠(yuǎn)離視角的感覺。接著調(diào)用Camera的rotateY()方法,讓視圖圍繞Y軸進(jìn)行旋轉(zhuǎn),從而產(chǎn)生立體旋轉(zhuǎn)的效果。最后通過Matrix來確定旋轉(zhuǎn)的中心點(diǎn)的位置。

有了這個(gè)工具類之后,我們就可以借助它非常簡單地實(shí)現(xiàn)中軸旋轉(zhuǎn)的特效了。接著創(chuàng)建一個(gè)圖片的實(shí)體類Picture,代碼如下所示:

public class Picture {   /**   * 圖片名稱   */  private String name;   /**   * 圖片對(duì)象的資源   */  private int resource;   public Picture(String name, int resource) {   this.name = name;   this.resource = resource;  }   public String getName() {   return name;  }   public int getResource() {   return resource;  }  } 

這個(gè)類中只有兩個(gè)字段,name用于顯示圖片的名稱,resource用于表示圖片對(duì)應(yīng)的資源。

然后創(chuàng)建圖片列表的適配器PictureAdapter,用于在ListView上可以顯示一組圖片的名稱,代碼如下所示:

public class PictureAdapter extends ArrayAdapter<Picture> {   public PictureAdapter(Context context, int textViewResourceId, List<Picture> objects) {   super(context, textViewResourceId, objects);  }   @Override  public View getView(int position, View convertView, ViewGroup parent) {   Picture picture = getItem(position);   View view;   if (convertView == null) {    view = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1,      null);   } else {    view = convertView;   }   TextView text1 = (TextView) view.findViewById(android.R.id.text1);   text1.setText(picture.getName());   return view;  }  } 

以上代碼都非常簡單,沒什么需要解釋的,接著我們打開或新建activity_main.xml,作為程序的主布局文件,代碼如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:id="@+id/layout"  android:layout_width="match_parent"  android:layout_height="match_parent"   >   <ListView   android:id="@+id/pic_list_view"   android:layout_width="match_parent"   android:layout_height="match_parent"   >  </ListView>    <ImageView   android:id="@+id/picture"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:scaleType="fitCenter"   android:clickable="true"   android:visibility="gone"   />  </RelativeLayout>

可以看到,我們?cè)赼ctivity_main.xml中放入了一個(gè)ListView,用于顯示圖片名稱列表。然后又加入了一個(gè)ImageView,用于展示圖片,不過一開始將ImageView設(shè)置為不可見,因?yàn)樯院笠ㄟ^中軸旋轉(zhuǎn)的方式讓圖片顯示出來。

最后,打開或新建MainActivity作為程序的主Activity,代碼如下所示:

public class MainActivity extends Activity {   /**   * 根布局   */  private RelativeLayout layout;   /**   * 用于展示圖片列表的ListView   */  private ListView picListView;   /**   * 用于展示圖片詳細(xì)的ImageView   */  private ImageView picture;   /**   * 圖片列表的適配器   */  private PictureAdapter adapter;   /**   * 存放所有圖片的集合   */  private List<Picture> picList = new ArrayList<Picture>();   @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   requestWindowFeature(Window.FEATURE_NO_TITLE);   setContentView(R.layout.activity_main);   // 對(duì)圖片列表數(shù)據(jù)進(jìn)行初始化操作   initPics();   layout = (RelativeLayout) findViewById(R.id.layout);   picListView = (ListView) findViewById(R.id.pic_list_view);   picture = (ImageView) findViewById(R.id.picture);   adapter = new PictureAdapter(this, 0, picList);   picListView.setAdapter(adapter);   picListView.setOnItemClickListener(new OnItemClickListener() {    @Override    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {     // 當(dāng)點(diǎn)擊某一子項(xiàng)時(shí),將ImageView中的圖片設(shè)置為相應(yīng)的資源     picture.setImageResource(picList.get(position).getResource());     // 獲取布局的中心點(diǎn)位置,作為旋轉(zhuǎn)的中心點(diǎn)     float centerX = layout.getWidth() / 2f;     float centerY = layout.getHeight() / 2f;     // 構(gòu)建3D旋轉(zhuǎn)動(dòng)畫對(duì)象,旋轉(zhuǎn)角度為0到90度,這使得ListView將會(huì)從可見變?yōu)椴豢梢?    final Rotate3dAnimation rotation = new Rotate3dAnimation(0, 90, centerX, centerY,       310.0f, true);     // 動(dòng)畫持續(xù)時(shí)間500毫秒     rotation.setDuration(500);     // 動(dòng)畫完成后保持完成的狀態(tài)     rotation.setFillAfter(true);     rotation.setInterpolator(new AccelerateInterpolator());     // 設(shè)置動(dòng)畫的監(jiān)聽器     rotation.setAnimationListener(new TurnToImageView());     layout.startAnimation(rotation);    }   });   picture.setOnClickListener(new OnClickListener() {    @Override    public void onClick(View v) {     // 獲取布局的中心點(diǎn)位置,作為旋轉(zhuǎn)的中心點(diǎn)     float centerX = layout.getWidth() / 2f;     float centerY = layout.getHeight() / 2f;     // 構(gòu)建3D旋轉(zhuǎn)動(dòng)畫對(duì)象,旋轉(zhuǎn)角度為360到270度,這使得ImageView將會(huì)從可見變?yōu)椴豢梢?,并且旋轉(zhuǎn)的方向是相反的     final Rotate3dAnimation rotation = new Rotate3dAnimation(360, 270, centerX,       centerY, 310.0f, true);     // 動(dòng)畫持續(xù)時(shí)間500毫秒     rotation.setDuration(500);     // 動(dòng)畫完成后保持完成的狀態(tài)     rotation.setFillAfter(true);     rotation.setInterpolator(new AccelerateInterpolator());     // 設(shè)置動(dòng)畫的監(jiān)聽器     rotation.setAnimationListener(new TurnToListView());     layout.startAnimation(rotation);    }   });  }   /**   * 初始化圖片列表數(shù)據(jù)。   */  private void initPics() {   Picture bird = new Picture("Bird", R.drawable.bird);   picList.add(bird);   Picture winter = new Picture("Winter", R.drawable.winter);   picList.add(winter);   Picture autumn = new Picture("Autumn", R.drawable.autumn);   picList.add(autumn);   Picture greatWall = new Picture("Great Wall", R.drawable.great_wall);   picList.add(greatWall);   Picture waterFall = new Picture("Water Fall", R.drawable.water_fall);   picList.add(waterFall);  }   /**   * 注冊(cè)在ListView點(diǎn)擊動(dòng)畫中的動(dòng)畫監(jiān)聽器,用于完成ListView的后續(xù)動(dòng)畫。   *   * @author guolin   */  class TurnToImageView implements AnimationListener {    @Override   public void onAnimationStart(Animation animation) {   }    /**    * 當(dāng)ListView的動(dòng)畫完成后,還需要再啟動(dòng)ImageView的動(dòng)畫,讓ImageView從不可見變?yōu)榭梢?   */   @Override   public void onAnimationEnd(Animation animation) {    // 獲取布局的中心點(diǎn)位置,作為旋轉(zhuǎn)的中心點(diǎn)    float centerX = layout.getWidth() / 2f;    float centerY = layout.getHeight() / 2f;    // 將ListView隱藏    picListView.setVisibility(View.GONE);    // 將ImageView顯示    picture.setVisibility(View.VISIBLE);    picture.requestFocus();    // 構(gòu)建3D旋轉(zhuǎn)動(dòng)畫對(duì)象,旋轉(zhuǎn)角度為270到360度,這使得ImageView將會(huì)從不可見變?yōu)榭梢?   final Rotate3dAnimation rotation = new Rotate3dAnimation(270, 360, centerX, centerY,      310.0f, false);    // 動(dòng)畫持續(xù)時(shí)間500毫秒    rotation.setDuration(500);    // 動(dòng)畫完成后保持完成的狀態(tài)    rotation.setFillAfter(true);    rotation.setInterpolator(new AccelerateInterpolator());    layout.startAnimation(rotation);   }    @Override   public void onAnimationRepeat(Animation animation) {   }   }   /**   * 注冊(cè)在ImageView點(diǎn)擊動(dòng)畫中的動(dòng)畫監(jiān)聽器,用于完成ImageView的后續(xù)動(dòng)畫。   *   * @author guolin   */  class TurnToListView implements AnimationListener {    @Override   public void onAnimationStart(Animation animation) {   }    /**    * 當(dāng)ImageView的動(dòng)畫完成后,還需要再啟動(dòng)ListView的動(dòng)畫,讓ListView從不可見變?yōu)榭梢?   */   @Override   public void onAnimationEnd(Animation animation) {    // 獲取布局的中心點(diǎn)位置,作為旋轉(zhuǎn)的中心點(diǎn)    float centerX = layout.getWidth() / 2f;    float centerY = layout.getHeight() / 2f;    // 將ImageView隱藏    picture.setVisibility(View.GONE);    // 將ListView顯示    picListView.setVisibility(View.VISIBLE);    picListView.requestFocus();    // 構(gòu)建3D旋轉(zhuǎn)動(dòng)畫對(duì)象,旋轉(zhuǎn)角度為90到0度,這使得ListView將會(huì)從不可見變?yōu)榭梢姡瑥亩氐皆c(diǎn)    final Rotate3dAnimation rotation = new Rotate3dAnimation(90, 0, centerX, centerY,      310.0f, false);    // 動(dòng)畫持續(xù)時(shí)間500毫秒    rotation.setDuration(500);    // 動(dòng)畫完成后保持完成的狀態(tài)    rotation.setFillAfter(true);    rotation.setInterpolator(new AccelerateInterpolator());    layout.startAnimation(rotation);   }    @Override   public void onAnimationRepeat(Animation animation) {   }   }  } 

MainActivity中的代碼已經(jīng)有非常詳細(xì)的注釋了,這里我再帶著大家把它的執(zhí)行流程梳理一遍。首先在onCreate()方法中調(diào)用了initPics()方法,在這里對(duì)圖片列表中的數(shù)據(jù)進(jìn)行初始化。然后獲取布局中控件的實(shí)例,并讓列表中的數(shù)據(jù)在ListView中顯示。接著分別給ListView和ImageView注冊(cè)了它們的點(diǎn)擊事件。

當(dāng)點(diǎn)擊了ListView中的某一子項(xiàng)時(shí),會(huì)首先將ImageView中的圖片設(shè)置為被點(diǎn)擊那一項(xiàng)對(duì)應(yīng)的資源,然后計(jì)算出整個(gè)布局的中心點(diǎn)位置,用于當(dāng)作中軸旋轉(zhuǎn)的中心點(diǎn)。之后創(chuàng)建出一個(gè)Rotate3dAnimation對(duì)象,讓布局以計(jì)算出的中心點(diǎn)圍繞Y軸從0度旋轉(zhuǎn)到90度,并注冊(cè)了TurnToImageView作為動(dòng)畫監(jiān)聽器。在TurnToImageView中監(jiān)測動(dòng)畫完成事件,如果發(fā)現(xiàn)動(dòng)畫已播放完成,就將ListView設(shè)為不可見,ImageView設(shè)為可見,然后再創(chuàng)建一個(gè)Rotate3dAnimation對(duì)象,這次是從270度旋轉(zhuǎn)到360度。這樣就可以實(shí)現(xiàn)讓ListView圍繞中軸旋轉(zhuǎn)消失,然后ImageView又圍繞中軸旋轉(zhuǎn)出現(xiàn)的效果了。

當(dāng)點(diǎn)擊ImageView時(shí)的處理其實(shí)和上面就差不多了,先將ImageView從360度旋轉(zhuǎn)到270度(這樣就保證以相反的方向旋轉(zhuǎn)回去),然后在TurnToListView中監(jiān)聽動(dòng)畫事件,當(dāng)動(dòng)畫完成后將ImageView設(shè)為不可見,ListView設(shè)為可見,然后再將ListView從90度旋轉(zhuǎn)到0度,這樣就完成了整個(gè)中軸旋轉(zhuǎn)的過程。

好了,現(xiàn)在全部的代碼都已經(jīng)完成,我們來運(yùn)行一下看看效果吧。在圖片名稱列表界面點(diǎn)擊某一項(xiàng)后,會(huì)中軸旋轉(zhuǎn)到相應(yīng)的圖片,然后點(diǎn)擊該圖片,又會(huì)中軸旋轉(zhuǎn)回到圖片名稱列表界面,如下圖所示:

Android中軸旋轉(zhuǎn)特效,Android圖片瀏覽器,Android旋轉(zhuǎn)特效

效果非常炫麗吧!本篇文章中的主要代碼其實(shí)都來自于API Demos里,我自己原創(chuàng)的部分并不多。而我是希望通過這篇文章大家都能夠大致了解Camera的用法,然后在下一篇文章中我將帶領(lǐng)大家使用Camera來完成更炫更酷的效果。

好了,今天的講解到此結(jié)束,有疑問的朋友請(qǐng)留言。

源碼下載,請(qǐng)點(diǎn)擊這里

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 临清市| 襄垣县| 阜新市| 玛沁县| 台北县| 乐平市| 丹凤县| 巴中市| 华蓥市| 平乐县| 苍溪县| 西安市| 奉贤区| 比如县| 漳州市| 鄢陵县| 辛集市| 紫金县| 襄樊市| 舞钢市| 岳阳市| 溆浦县| 江城| 灌阳县| 浮山县| 马尔康县| 北流市| 观塘区| 渝北区| 巨鹿县| 清丰县| 重庆市| 和田市| 太保市| 怀化市| 南安市| 抚远县| 沐川县| 绍兴市| 英超| 德保县|