您好,登錄后才能下訂單哦!
剛剛學習Android,用Gallery作了一個小demo,用來記錄一下。
package com.example.galleryex02; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setTitle("電影海報"); Gallery gallery = (Gallery) findViewById(R.id.gallery1); MyGalleryAdapter galAdapter = new MyGalleryAdapter(this); gallery.setAdapter(galAdapter); } public class MyGalleryAdapter extends BaseAdapter { Context context; Integer[] posterID = { R.drawable.mov11, R.drawable.mov12, R.drawable.mov13, R.drawable.mov14, R.drawable.mov15, R.drawable.mov16, R.drawable.mov17, R.drawable.mov18, R.drawable.mov19, R.drawable.mov20 }; public MyGalleryAdapter(Context c) { context = c; } public int getCount() { // TODO Auto-generated method stub return posterID.length; } public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } public long getItemId(int position) { // TODO Auto-generated method stub return 0; } public View getView(int position, View convertView, ViewGroup parent) { ImageView imageview = new ImageView(context); imageview.setLayoutParams(new Gallery.LayoutParams(100, 150)); //設置image大小 imageview.setScaleType(ImageView.ScaleType.FIT_CENTER); imageview.setPadding(5, 5, 5, 5); imageview.setImageResource(posterID[position]); final int pos = position; //點擊galley的image的時候 imageview.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { ImageView ivPoster = (ImageView) findViewById(R.id.ivPoster); ivPoster.setScaleType(ImageView.ScaleType.FIT_CENTER); ivPoster.setImageResource(posterID[pos]); return false; } }); return imageview; } } }
效果如下:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。