就是想做一个相册的小功能,希望点击的图片可以比其他未选中的图片稍大,
贴出我现在写的吧,不知道为什么每次选择图片的时候会无限放大,最后内存溢出....
package com.max.pictrueDemo;import java.text.SimpleDateFormat;import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;public class MainActivity extends Activity {
private Gallery gallery = null;
private Button bt_back = null;
private Button bt_next = null;
private TextView text_Time = null;
private TextView text_Title = null;
private ImageAdapter imageAdapter = null;
private ImageView imageView = null;
private LinearLayout mLinearLayout;
private int index = Integer.MAX_VALUE%2;
private Bitmap bm;
private float scaleWidth=1,scaleHeight=1;

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");   
String date=sdf.format(new java.util.Date());  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mLinearLayout = (LinearLayout)this.findViewById(R.id.LinearLayout_image);
        gallery = (Gallery)this.findViewById(R.id.Gallery01);
        bt_back = (Button)this.findViewById(R.id.Button_back);
        bt_next = (Button)this.findViewById(R.id.Button_next);
        text_Time = (TextView)this.findViewById(R.id.TextView_time);
        text_Title = (TextView)this.findViewById(R.id.TextView_title);
        imageView = (ImageView)this.findViewById(R.id.ImageView_Change);
        imageAdapter = new ImageAdapter(this);
        bt_back.setOnClickListener(listener);
        bt_next.setOnClickListener(listener);
        gallery.setSelection(200);
        gallery.setAdapter(imageAdapter);
        gallery.setOnItemSelectedListener(new OnItemSelectedListener() { @Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
final Integer resid = imageAdapter.myImageids[arg2%imageAdapter.myImageids.length];
System.out.println(resid);
bm = BitmapFactory.decodeResource(MainActivity.this.getResources(),resid);

big(imageView);
text_Title.setText(arg1.getResources().getResourceName(resid).split("/")[1]);

} @Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}        

});        
        text_Time.setText(date);
    }
    OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.Button_back:
gallery.setSelection(--index);
return;
case R.id.Button_next:
gallery.setSelection(++index);
return;
}
}   
};
public void big(ImageView i){

int bmWidth = bm.getWidth();
int bmHeight = bm.getHeight();

double scale = 1.5;

scaleWidth = (float)(scaleWidth*scale);
scaleHeight = (float)(scaleHeight*scale);

Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizeBmp = Bitmap.createBitmap(bm,0,0,bmWidth,bmHeight,matrix,true);

i.setImageBitmap(resizeBmp);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/LinearLayout_image"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Gallery 
android:id="@+id/Gallery01" 
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Gallery>
<ImageView 
android:id="@+id/ImageView_Change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<TableLayout 
android:id="@+id/TableLayout01" 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1">
<TableRow 
android:id="@+id/TableRow_Button" 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:id="@+id/Button_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上一个"/>
<Button android:id="@+id/Button_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一个"/>
</TableRow>
<TableRow 
android:id="@+id/TableRow_TextView" 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView 
android:id="@+id/TextView_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="time"
android:gravity="center_horizontal"/>
<TextView 
android:id="@+id/TextView_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="title"
android:gravity="center_horizontal"/>
</TableRow>
</TableLayout>   
</LinearLayout>

解决方案 »

  1.   

    这肯定不对啊,每次都是放大1.5倍,那肯定是越来越大,   public void big(ImageView i){
            
            int bmWidth = bm.getWidth();
            int bmHeight = bm.getHeight();
            
            double scale = 1.5;
            
            int Width = (float)(scaleWidth*scale);
            int Height = (float)(scaleHeight*scale);

            
            Matrix matrix = new Matrix();
            matrix.postScale(Width, Height);
            Bitmap resizeBmp = Bitmap.createBitmap(bm,0,0,bmWidth,bmHeight,matrix,true);
            
            i.setImageBitmap(resizeBmp);
        }
      

  2.   

    呵呵,谢谢,我怎么就没注意到这个问题呢,再问一下如果我想把当前选中的item删除应该怎么做,就是Gallery中当前的图片不让其显示
      

  3.   

    哦,是java.lang.IllegalArgumentException,是原来的代码报内存溢出了,不好意思了
      

  4.   

    如果要删除,那就直接控制Adapter去remove指定一项就行了,然后删除了调用notifyDataSetChanged更新
      

  5.   


    没有看到每次都1.5倍啊。
    原图是没有变的,每个都是从原图生成一个1.5位的图显示在ImageView上。
    原图始终没有变!!!!!!!哪来的无限循环啊????
      

  6.   

    LS...每次点击都是去放大1.5倍,也就是说第一次点击后是1.5,第二次点击就是1.5x1.5,第三次就是放大1.5的3此方
      

  7.   

    package com.max.pictrueDemo;import android.content.Context;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Gallery;
    import android.widget.ImageView;public class ImageAdapter extends BaseAdapter {
    private Context context;
    float scaleW;
    float scaleH; public ImageAdapter(Context c) {
    this.context = c;
    } int[] myImageids = { R.drawable.chance_of_rain, R.drawable.chance_of_snow,
    R.drawable.chance_of_storm, R.drawable.cloudy, R.drawable.dust,
    R.drawable.fog, R.drawable.haze }; @Override
    public int getCount() {
    // TODO Auto-generated method stub
    return Integer.MAX_VALUE;
    } @Override
    public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
    } @Override
    public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
    } @Override
    public View getView(int position, View convertView, ViewGroup parent) { ImageView i = new ImageView(context);
    if (convertView == null) {
    i = new ImageView(context);
    } else {
    i = (ImageView) convertView;
    }
    if (position < 0) {
    position = position + myImageids.length;
    }
    i.setDrawingCacheEnabled(true);
    i.setImageResource(myImageids[position % myImageids.length]);
    i.setScaleType(ImageView.ScaleType.CENTER_CROP);
    i.setLayoutParams(new Gallery.LayoutParams(128, 128));
    i.setPadding(8, 8, 8, 8); return i;
    }
    }
    求详解....
      

  8.   

    ImageAdapter不是set给了gallery么?
    那ImageAdapter里自己写个remove的方法,getView控制显示,显示的内容肯定是自己定义的,比如一个list数组,remove里就是去删除指定的数组元素,然后调用notifyDataSetChanged,就会自己刷新,刷新时进入getView,这时候list数组里的内容已经remove了,自然就显示不出来了
      

  9.   

    小弟菜鸟,实在没想出来怎么在getView里删除特定数组元素,只知道ImageAdapter.this.notifyDataSetChanged();要写在线程里时刻监听,我在想是不是在gallery.setOnItemSelectedListener里得到当前的Gallery里的ImageView对象,然后删除之,请明示remove怎么写啊,谢谢了~
      

  10.   

    谢谢,这个问题已经解决了,现在想把Gallery里当前显示的ImageView删掉,不知道有什么好的方法?
      

  11.   

    小弟菜鸟,实在没想出来怎么在getView里删除特定数组元素,只知道ImageAdapter.this.notifyDataSetChanged();要写在线程里时刻监听,我在想是不是在gallery.setOnItemSelectedListener里得到当前的Gallery里的ImageView对象,然后删除之,请明示remove怎么写啊,谢谢了~...这样也可以反正每次getview显示时都去获取ImageView对象,可以根据id直接干掉。parent可以调用
    void  removeView(View view)
    void  removeViewAt(int index)
    这两个来移除,反正parent里已经有了所有的ImageView,不要显示你就remove就行
      

  12.   

    我在OnItemSelectedListener直接写
    gallery.removeView(arg1);

    gallery.removeViewAt(arg2);抛异常了,请问怎么回事?郁闷了
      

  13.   

    我还试图这样写:imageView2 = (ImageView)gallery.getChildAt(arg2);
                    mLinearLayout.removeView(imageView2);
    也不管用....
      

  14.   

    这么试试,Imageview可以设置id,指定给每个Imageview都setTag不同值,然后删除的时候判断Imageview的tag是不是正确的,然后再去remove
      

  15.   

    在OnItemSelectedListener里各种不给力啊,您还是说说在Adapter里的删除数据的remove怎么写吧...谢谢了
      

  16.   

    删除数据,然后notifyDatasetChanged
      

  17.   

    问题是,删除ImageView的时候,程序就异常退出了