android中imageview图片旋转出错,请大神看下 30
publicclassGamedemo2ActivityextendsActivity{/**Calledwhentheactivityisfirstcreated.*/...
public class Gamedemo2Activity extends Activity {
/** Called when the activity is first created. */
ImageView iv;
Button bt;
Bitmap bmp;
int w,h;
float curDegrees;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv=(ImageView)findViewById(R.id.iv);
bt=(Button)findViewById(R.id.bt);
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.lord);
curDegrees=10.0f;
w = bmp.getWidth();
h = bmp.getHeight();
iv.setImageBitmap(bmp);
bt.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
turn();}
});
}
public void turn(){
w = bmp.getWidth();
h = bmp.getHeight();
Matrix mt = new Matrix();
mt.setRotate(curDegrees = curDegrees + 5);
Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,w,h,mt,true);
iv.setImageBitmap(resizeBmp);
}
} 展开
/** Called when the activity is first created. */
ImageView iv;
Button bt;
Bitmap bmp;
int w,h;
float curDegrees;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv=(ImageView)findViewById(R.id.iv);
bt=(Button)findViewById(R.id.bt);
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.lord);
curDegrees=10.0f;
w = bmp.getWidth();
h = bmp.getHeight();
iv.setImageBitmap(bmp);
bt.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
turn();}
});
}
public void turn(){
w = bmp.getWidth();
h = bmp.getHeight();
Matrix mt = new Matrix();
mt.setRotate(curDegrees = curDegrees + 5);
Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,w,h,mt,true);
iv.setImageBitmap(resizeBmp);
}
} 展开
3个回答
展开全部
程序报的错误是java.lang.OutOfMemoryError: bitmap size exceeds VM budget么?除了这个其他地方没什么错,在Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,w,h,mt,true);
iv.setImageBitmap(resizeBmp);这里注意resizeBmp的recycle();
不过我不太会用recycle(),所以我的做法是用下面这几句代替:
WeakReference<Bitmap> wr = new WeakReference<Bitmap>(Bitmap.createBitmap(bmp, 0, 0, w, h, mt, true));
iv.setImageBitmap(wr.get());
System.gc();
希望对你有所帮助:)
iv.setImageBitmap(resizeBmp);这里注意resizeBmp的recycle();
不过我不太会用recycle(),所以我的做法是用下面这几句代替:
WeakReference<Bitmap> wr = new WeakReference<Bitmap>(Bitmap.createBitmap(bmp, 0, 0, w, h, mt, true));
iv.setImageBitmap(wr.get());
System.gc();
希望对你有所帮助:)
展开全部
内存溢出问题BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize =4;
bitmap=BitmapFactory.decodeByteArray(data, 0, data.length, opts);
opts.inSampleSize =4;
bitmap=BitmapFactory.decodeByteArray(data, 0, data.length, opts);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
代码没问题,应该是你图片资源太大了,造成内存溢出。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询