Android編程仿Iphone拖動相片特效Gallery的簡單應(yīng)用示例
本文實例講述了Android編程仿Iphone拖動相片特效Gallery的簡單應(yīng)用。分享給大家供大家參考,具體如下:
Step 1:準(zhǔn)備圖片素材.
將icon2,icon3,icon4,icon5,icon6五張圖片導(dǎo)入res/drawable里加上icon.png本身一共有6張圖片.
Step 2:新建Android工程,命名為GalleryDemo.
Step 3:設(shè)計UI,修改main.xml代碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/white" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/myTextView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:gravity="center_vertical|center_horizontal" /> <Gallery android:id="@+id/myGallery1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="bottom" /> </LinearLayout>
Step 4:設(shè)計主程序類GalleryDemo.Java代碼如下:
package com.android.test; import com.android.test.R.drawable; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class GalleryDemo extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ((Gallery) findViewById(R.id.myGallery1)).setAdapter(new ImageAdapter( this)); } public class ImageAdapter extends BaseAdapter { /* 類成員 myContext為Context父類 */ private Context myContext; /* 使用res/drawable圖片作為圖片來源 */ private int[] myImageIds = { drawable.icon, drawable.icon2, drawable.icon3, drawable.icon4, drawable.icon5, drawable.icon6}; /* 構(gòu)造器只有一個參數(shù),即要存儲的Context */ public ImageAdapter(Context c) { this.myContext = c; } /* 返回所有已定義的圖片總數(shù)量 */ public int getCount() { return this.myImageIds.length; } /* 利用getItem方法,取得目前容器中圖像的數(shù)組ID */ public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } /* 取得目前欲顯示的圖像View,傳入數(shù)組ID值使之讀取與成像 */ public View getView(int position, View convertView, ViewGroup parent) { /* 創(chuàng)建一個ImageView對象 */ ImageView i = new ImageView(this.myContext); i.setImageResource(this.myImageIds[position]); i.setScaleType(ImageView.ScaleType.FIT_XY); /* 設(shè)置這個ImageView對象的寬高,單位為dip */ i.setLayoutParams(new Gallery.LayoutParams(120, 120)); return i; } /* 依據(jù)距離中央的位移量 利用getScale返回views的大小(0.0f to 1.0f) */ public float getScale(boolean focused, int offset) { /* Formula: 1 / (2 ^ offset) */ return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset))); } } }
Step 5:run it,效果如下圖:
注明:該代碼基本參照Android SDK開發(fā)范例代碼大全
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- android 添加隨意拖動的桌面懸浮窗口
- Android 仿淘寶、京東商品詳情頁向上拖動查看圖文詳情控件DEMO詳解
- Android 可拖動的seekbar自定義進(jìn)度值
- Android編程之控件可拖動的實現(xiàn)方法
- Android編程實現(xiàn)圖標(biāo)拖動效果的方法
- Android實現(xiàn)ImageView圖片縮放和拖動
- Android自定義View實現(xiàn)拖動選擇按鈕
- 在android中實現(xiàn)類似uc和墨跡天氣的左右拖動效果
- Android編程實現(xiàn)圖片的瀏覽、縮放、拖動和自動居中效果
- Android UI控件之Gallery實現(xiàn)拖動式圖片瀏覽效果
相關(guān)文章
Android下拉列表(Spinner)效果(使用C#和Java分別實現(xiàn))
這篇文章主要介紹了Android下拉列表(Spinner)效果(使用C#和Java分別實現(xiàn)),本文直接給出效果圖和兩種語言的實現(xiàn)代碼及布局代碼,需要的朋友可以參考下2015-06-06Android編程動態(tài)修改RelativeLayout寬高的方法
這篇文章主要介紹了Android編程動態(tài)修改RelativeLayout寬高的方法,涉及Android動態(tài)布局的相關(guān)技巧,需要的朋友可以參考下2015-12-12Android自定義View實現(xiàn)圓環(huán)帶數(shù)字百分比進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義View實現(xiàn)圓環(huán)帶數(shù)字百分比進(jìn)度條,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12解析android中的幫助、about、關(guān)于作者、HELP等提示頁面
本篇文章是對android中的幫助、about、關(guān)于作者、HELP等提示頁面進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06