Android使用Gallery實現(xiàn)照片拖動的特效
今天要分享一個非常簡單的功能:
使用Android原生控件Gallery實現(xiàn)照片拖動的特效
實現(xiàn)思路如下: 在布局文件中定義一個Gallery控件 由于要顯示多張圖,為了方便,我直接引用了Android原生的圖片資源 Gallery只是一個控件,為了將圖片數(shù)據(jù)跟控件進行綁定,還需要一個繼承BaseAdapter的自定義適配器 源碼如下:1、主activity和自定義內(nèi)部類ImageAdapter:
import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;import com.example.memorydemo.R;public class SimpleGallery extends Activity { private static final String TAG = 'SimpleGallery'; @Override protected void onCreate(Bundle onSavedInstance) { super.onCreate(onSavedInstance); setContentView(R.layout.simple_gallery_layout); Gallery gallery = findViewById(R.id.gallery); gallery.setAdapter(new ImageAdapter(this)); } private class ImageAdapter extends BaseAdapter { // 這里我們使用Android原生的資源圖標 private int[] imageIds = {android.R.drawable.btn_minus,android.R.drawable.btn_radio,android.R.drawable.ic_lock_idle_low_battery,android.R.drawable.ic_menu_camera }; private Context mContext; public ImageAdapter(Context context) { mContext = context; } @Override public int getCount() { return imageIds.length; } @Override public Object getItem(int position) { return imageIds[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) {Log.i(TAG, 'convertView is null, create new imageview');imageView = new ImageView(mContext); } else {Log.i(TAG, 'Cast convertView to ImageView');imageView = (ImageView) convertView; } imageView.setImageResource(imageIds[position]); imageView.setScaleType(ImageView.ScaleType.FIT_XY); // 注意這里要用Gallery.LayoutParams作為布局參數(shù)類型,源碼中給出了建議(Views given to the Gallery should use // Gallery.LayoutParams s their ayout parameters type)// 由于Android原生圖片很小,我將高度設(shè)置為 500,方便看效果 imageView.setLayoutParams(new Gallery.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 500)); return imageView; } }}
2、布局文件 simple_gallery_layout.xml如下:
<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' android:layout_width='match_parent' android:layout_height='match_parent'> <Gallery android: android:layout_width='match_parent' android:layout_height='match_parent' /></LinearLayout>注意:
Gallery控件其實已經(jīng)被廢棄了,建議用 HorizontalScrollView 和 ViewPager 代替,源碼中是這么解釋的:
@deprecated This widget is no longer supported. Other horizontally scrolling widgets include {@link HorizontalScrollView} and {@link android.support.v4.view.ViewPager} from the support library.
后續(xù)會分享 HorizontalScrollView 和 ViewPager這兩個控件是如何使用的。
以上就是Android使用Gallery實現(xiàn)照片拖動的特效的詳細內(nèi)容,更多關(guān)于Android 照片拖動特效的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
