我正在制作Android应用程序,我需要在cavas中加载图像(位图)并使用"捏缩放"手势调整大小.但是,当图像超过一定大小时,应用程序崩溃(OutOfMemory异常).如何优化图像的加载和操作?
要加载我使用的图像:
BitmapFactory.decodeResource (ctx.getResources (), R.drawable.image)
Run Code Online (Sandbox Code Playgroud)
绘制它:
imgCanvas.drawBitmap (image, posX, posY, null),
Run Code Online (Sandbox Code Playgroud)
要改变它的大小:
Bitmap.createScaledBitmap (originalBitmap, neww, NEWH, true);
Run Code Online (Sandbox Code Playgroud) 我正在尝试从屏幕上的特定区域创建位图.例如,在下图中,我如何捕获下面的窗口区域并将其转换为位图?

我知道你可以使用setDrawingCacheEnabled(true),但是当我想要的只是视图中的一个区域时,它会捕获整个视图.
我正在尝试在Google Maps v2中设置标记图标.我正在通过网络下载一些图像并在代码中更改其背景.之后我将它们设置为标记的图标.在第一次创建地图时,它工作正常,但在轮换后有一个例外.
Android版本我运行:4.3
我的代码如下:
UrlImageViewHelper.loadUrlDrawable(TuvaletlerMapActivity.this,
iconUrl, new UrlImageViewCallback() {
@Override
public void onLoaded(ImageView iv, Bitmap bm,
String arg2, boolean arg3) {
Bitmap bitmap = VenuesHelper.iconizeBitmap(bm);
marker.setIcon(BitmapDescriptorFactory
.fromBitmap(bitmap));
}
});
Run Code Online (Sandbox Code Playgroud)
并且VenuesHelper.iconizeBitmap()是如下:
public static Bitmap iconizeBitmap(Bitmap bm) {
Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(),
bm.getConfig());
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.parseColor("#33B5E5"));
canvas.drawBitmap(bm, 0, 0, null);
return bitmap;
}
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪如下:
08-07 10:16:50.684: E/AndroidRuntime(19001): FATAL EXCEPTION: main
08-07 10:16:50.684: E/AndroidRuntime(19001): java.lang.IllegalArgumentException: Released unknown bitmap reference
08-07 10:16:50.684: E/AndroidRuntime(19001): at maps.as.i.a(Unknown Source)
08-07 …Run Code Online (Sandbox Code Playgroud) android bitmap google-maps-markers google-maps-android-api-2
有没有办法在Eclipse上调试canvas/bitmaps?我想知道当我在特定行设置断点时是否可以查看当前画布/位图的外观.
我正在尝试绘制用户加载的位图.这是在onCreate.
imageView = (ImageView) this.findViewById(R.id.ImageView);
Display currentDisplay = getWindowManager().getDefaultDisplay();
float dw = currentDisplay.getWidth();
float dh = currentDisplay.getHeight();
bitmap = Bitmap.createBitmap((int) dw, (int) dh,
Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
paint = new Paint();
paint.setColor(Color.BLUE);
imageView.setImageBitmap(bitmap);
imageView.setOnTouchListener(this);
Run Code Online (Sandbox Code Playgroud)
下一个代码是当用户按下按钮以获取位图并且在成功检索位图之后.它在" onActivityResult" 结尾
canvas = new Canvas(bitmap);
paint = new Paint();
paint.setColor(Color.BLUE);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
imageView.setOnTouchListener(this);
Run Code Online (Sandbox Code Playgroud)
我能够绘制在期间制作的空白位图onCreate,但是当用户加载新位图并尝试绘制它时不会显示.但是加载了新的位图.
我也厌倦了
canvas = new Canvas(BitmapFactory.decodeFile(picturePath));
Run Code Online (Sandbox Code Playgroud)
然后它给了我错误.
谁知道我做错了什么?
编辑:这是日志文件:
10-25 03:19:31.409: W/System.err(1971): java.lang.Exception: java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
10-25 03:19:31.409: W/System.err(1971): at com.example.milestone2.Draw.onActivityResult(Draw.java:148)
10-25 …Run Code Online (Sandbox Code Playgroud) 我正在尝试将图像从URL加载到ImageView但发生错误:SkImageDecoder :: Factory返回null.我该如何解决?
这是我的代码:
private class LoadImageFromURL extends AsyncTask<String, Void, Bitmap>{
ImageView bitmapImgView;
public LoadImageFromURL(ImageView bmImgView){
bitmapImgView = bmImgView;
}
@Override
protected Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
String urlStr = params[0];
Bitmap img = null;
try {
URL url = new URL(urlStr);
InputStream inputStream = url.openConnection().getInputStream();
//Options bmFactoryOpt = new Options();
//bmFactoryOpt.inJustDecodeBounds = false;
img = BitmapFactory.decodeStream(inputStream);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种从c#托管的GDI +库中检测 c#位图的边缘空白的解决方案.
图像可以是透明的或白色的,400x图像中的大多数是8000x8000px,边缘周围有大约2000px的空白.
找出边缘,x,y,高度和宽度坐标的最有效方法是什么?我尝试逐像素,但发现它很慢.
更新到解决方案 - 添加左/右/上/下边界
图像细节中心图像的问题现在裁剪任何透明(0%)或白色(#FFFFFF)像素.
var top = bitmap.Height;
var left = bitmap.Width;
var right = 0;
var bottom = 0;
Run Code Online (Sandbox Code Playgroud)
...
var pData = pData0 + (y * data.Stride) + (x * 4);
var xyAlpha = pData[3];
var xyBlue = pData[0];
var xyGreen = pData[1];
var xyRed = pData[2];
if ((xyAlpha > 0) || (xyRed != 255 && xyGreen != 255 && xyBlue != 255)) { …Run Code Online (Sandbox Code Playgroud) 在BlackBerry中,在内存使用和性能方面使用Bitmap类或EncodedImage会更好吗?有关使用这些课程的具体提示吗?
所有,
之后,我注意到有缓慢getPixel而setPixel有(不知道哪一个,都猜不涡轮增压)我赶紧编码的容器Bitmap,使用int[]阵列来处理位图操作.
已经 - 它明显更快,但这还不够.请问您如何进一步加快速度?
我的想法是跟踪setPixel函数"脏"的内容并仅更新调用Bitmap时的这一部分getBitmap()......不清楚如何设置setPixels参数(我猜测偏移和步幅的东西).
还 - 任何更快的食谱?
感谢您提前帮助!
import android.graphics.Bitmap;
public class DrawableBitmapContainer {
private Bitmap image;
private int width, height;
private int[] pixels;
public DrawableBitmapContainer(Bitmap _source ){
image = _source;
width = image.getWidth();
height = image.getHeight();
pixels = new int[width*height];
image.getPixels(pixels,0,width,0,0,width,height);
}
public int getPixel(int x,int y){
return pixels[x+y*width];
}
public void setPixel(int x,int y, int color){
pixels[x+y*width]=color;
}
public Bitmap …Run Code Online (Sandbox Code Playgroud) 我想在图像上显示一个点和一个文本.我已经尝试了几个关于覆盖位图的教程,但它似乎不起作用.这是显示背景图像的代码.
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.roomplan);
mIV = (ImageView)findViewById(R.id.ImageView01);
mIV.setImageBitmap(mBitmap);
mIV.invalidate();
btnDraw = (Button)findViewById(R.id.Button01);
btnDraw.setOnClickListener(this);
Run Code Online (Sandbox Code Playgroud)
然后在OnClickListener上,我定义另一个位图并绘制点和文本.
Bitmap bmOverlay = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(),
Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bmOverlay);
Paint paint = new Paint();
paint.setColor(Color.CYAN);
paint.setTextSize(20);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
canvas.drawText("You are here", 100, 100, paint);
canvas.drawPoint(30.0f, 50.0f, paint);
canvas.drawBitmap(bmOverlay, 0, 0, null);
Run Code Online (Sandbox Code Playgroud)
即使我删除图像,背景图像上也不会显示任何内容.有什么提示吗?
更新:工作代码
// get a reference to the background image
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.raumplan_isst);
mIV = (ImageView)findViewById(R.id.ImageView01);
// create a mutable bitmap with the same size as the background image
Bitmap bmOverlay …Run Code Online (Sandbox Code Playgroud) bitmap ×10
android ×8
graphics ×3
image ×2
algorithm ×1
blackberry ×1
c# ×1
canvas ×1
draw ×1
eclipse ×1
imageview ×1
java ×1
optimization ×1
overlay ×1
performance ×1