gallery点击无响应

发布于 2021-11-13 06:51:25 字数 4706 浏览 845 评论 2

package suhq.android.projects.make;

import android.R.integer;
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.view.Window;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.graphics.Bitmap;  	
import android.graphics.Color;
import android.graphics.drawable.Drawable;  
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.ViewSwitcher.ViewFactory;
import android.widget.ArrayAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
public class Iphoto extends Activity implements ViewFactory{
	ImageSwitcher imgSwitcher;
	Gallery gallery;
	private int[] imgSource;
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.i_photo);
		setupViews();
	} 
	private void setupViews(){  
			imgSource=new int[]{R.drawable.lump_background_1,R.drawable.lump_background_2,R.drawable.lump_background_3,
	        					R.drawable.lump_background_4,R.drawable.lump_background_5,R.drawable.lump_background_6,
	        					R.drawable.lump_background_7,R.drawable.lump_background_8,R.drawable.lump_background_9,
	        					R.drawable.lump_background_10,R.drawable.lump_background_11,R.drawable.lump_background_12};
	        imgSwitcher=(ImageSwitcher)findViewById(R.id.imgSwitcher);
	        gallery=(Gallery)findViewById(R.id.gallery);
	        imgSwitcher.setFactory(this);
	        imgSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.out_to_left));
	        imgSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.in_from_right));
	        gallery.setAdapter(new ImageAdapter(this, imgSource));   
	        gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
	        	public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
	    			Drawable drawable=Iphoto.this.getResources().getDrawable(imgSource[position]);
	    			Bitmap bitmap = ImageUtil.drawableToBitmap(drawable);   
	    	        //缩放图片   
	    	        //Bitmap zoomBitmap = ImageUtil.zoomBitmap(bitmap, 100, 100);  
	    	        //获取圆角图片   
	    	       // Bitmap roundBitmap = ImageUtil.getRoundedCornerBitmap(zoomBitmap, 10.0f);  
	    	        //获取倒影图片   
	    	        Bitmap reflectBitmap = ImageUtil.createReflectionImageWithOrigin(bitmap);
	    	        drawable=ImageUtil.bitmapToDrawable(Iphoto.this,reflectBitmap);
	    	        imgSwitcher.setImageDrawable(drawable);Log.i("suhq24","dfgfggh");
	    		//}
	    	}
	    	public void onNothingSelected(AdapterView<?> adapterView) {
	    		
	    	}
	    	});
	        //这里可以让Bitmap再转化为Drawable   
//	      Drawable roundDrawable = new BitmapDrawable(roundBitmap);          
//	      Drawable reflectDrawable = new BitmapDrawable(reflectBitmap);          
//	      mImageView01.setBackgroundDrawable(roundDrawable);   
//	      mImageView02.setBackgroundDrawable(reflectDrawable);   
	        
	    } 
	public View makeView() {
		ImageView imgView=new ImageView(this);
		//imgView.setBackgroundColor(Color.CYAN);
		imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
		imgView.setLayoutParams(new ImageSwitcher.LayoutParams(180, 260));
		return imgView;
	}
	
	
	

	protected class ImageAdapter extends BaseAdapter{
		private Context context;
		private int[] imgIds;
		public ImageAdapter(Context context,int[] imgIds) {
			this.imgIds=imgIds;
			this.context=context;
		}
		public int getCount(){
			return imgIds.length;
		}
		public Object getItem(int item){
			return item;
		}
		public long getItemId(int id){
			return id;
		}
		public View getView(final int position, View convertView,ViewGroup parent){
			ImageView imgView;
			if (convertView==null) {
				imgView=new ImageView(context);
				imgView.setAdjustViewBounds(false);
				imgView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));//设置ImageView边界布局
				imgView.setClickable(true);
				imgView.setPadding(0, 2, 8, 2);
				imgView.setImageResource(imgIds[position]);
			}else {
				imgView=(ImageView)convertView;
			}
			
			return imgView;
		}
	}
 }


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情栀口红 2021-11-18 07:09:19

我要的是onItemSelectedLisener 。该事件在我刚运行程序时执行了一次,之后要选择其他图片时无响应

自此以后,行同陌路 2021-11-16 13:58:15

点击事件是setOnClickListener

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文