android圖片拖動Gallery

來源:互聯網
上載者:User

1.Gallery可以拖動圖片,實現很炫的效果,如下,可以向左右拖動:

2.布局代碼
<?xml version="1.0" encoding="utf-8"?><Gallery xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/Gallery"    android:layout_width="fill_parent"    android:layout_height="wrap_content"></Gallery>
3.用一個類繼承自BaseAdapter來充當容器

 

public class ImageAdapter extends BaseAdapter { private Context mContext; private Integer[] mImageIds = { R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7, R.drawable.img8 }; public ImageAdapter(Context c){ this.mContext = c; } @Override public int getCount() { return mImageIds.length;//擷取圖片的個數 } @Override public Object getItem(int position) { return position;//擷取圖片在庫中的位置 } @Override public long getItemId(int position) { return position;//擷取圖片在庫中的位置 } @Override public View getView(int position, View convertView, ViewGroup parent) { Log.i("TAG", position+""); ImageView imageView = new ImageView(mContext); imageView.setImageResource(mImageIds[position]); imageView.setLayoutParams(new Gallery.LayoutParams(100,100));//設定圖片大小 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);//設定顯示比例類型 return imageView; } }

 

4.使用時的java代碼:public class Test_Gallery extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Gallery g = (Gallery) this.findViewById(R.id.Gallery); g.setAdapter(new ImageAdapter(this)); g.setBackgroundResource(R.drawable.bg0); g.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(Test_Gallery.this, "選擇了"+(arg2+1)+"號圖片", Toast.LENGTH_SHORT).show(); } }); }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.