就是想做一个相册的小功能,希望点击的图片可以比其他未选中的图片稍大,
贴出我现在写的吧,不知道为什么每次选择图片的时候会无限放大,最后内存溢出....
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>
贴出我现在写的吧,不知道为什么每次选择图片的时候会无限放大,最后内存溢出....
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>
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);
}
没有看到每次都1.5倍啊。
原图是没有变的,每个都是从原图生成一个1.5位的图显示在ImageView上。
原图始终没有变!!!!!!!哪来的无限循环啊????
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;
}
}
求详解....
那ImageAdapter里自己写个remove的方法,getView控制显示,显示的内容肯定是自己定义的,比如一个list数组,remove里就是去删除指定的数组元素,然后调用notifyDataSetChanged,就会自己刷新,刷新时进入getView,这时候list数组里的内容已经remove了,自然就显示不出来了
void removeView(View view)
void removeViewAt(int index)
这两个来移除,反正parent里已经有了所有的ImageView,不要显示你就remove就行
gallery.removeView(arg1);
或
gallery.removeViewAt(arg2);抛异常了,请问怎么回事?郁闷了
mLinearLayout.removeView(imageView2);
也不管用....