标签: android-bitmap

在销毁时释放特定活动的内存

我有一个启动器Activity加载和调整大位图的大小,因为它是打开时的背景.

每当按下后退按钮时,Activity就是destroyed.但我认为记忆尚未公布.

当我打开应用程序时,点击后退按钮再次打开它(反复),我会得到一个OutOfMemoryError.

我这个新手的问题,对不起,但我不知道我怎么释放每当记忆Activitydestroyed

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);

    //MARK - movingBackgroundImageView
    movingBackgroundImageView = (ImageView) findViewById(R.id.movingBackgroundImageView);
    movingBackgroundImageView.setColorFilter(Color.argb(255, 255, 255, 255));
    movingBackgroundImageView.setScaleType(ImageView.ScaleType.MATRIX);
    movingBackgroundImageView.setAlpha(0.28f);

    prepareBackgroundAnimation();
}

private void prepareBackgroundAnimation() {

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);

    screenWidth = displaymetrics.widthPixels;
    screenHeight = displaymetrics.heightPixels;

    movingImageHeight = displaymetrics.heightPixels;
    movingImageWidth = 1920.0 / 1080.0 * movingImageHeight;

    bitmapImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.moving_background_image);
    scaledBitmap = bitmapImage.createScaledBitmap(bitmapImage, (int) movingImageWidth, (int) movingImageHeight, false);
    movingBackgroundImageView.setImageBitmap(scaledBitmap);

    backgroundImageInBeginning …
Run Code Online (Sandbox Code Playgroud)

android out-of-memory android-memory android-studio android-bitmap

20
推荐指数
2
解决办法
2万
查看次数

水平或垂直翻转位图图像

通过使用此代码,我们可以旋转图像:

public static Bitmap RotateBitmap(Bitmap source, float angle) {
      Matrix matrix = new Matrix();
      matrix.postRotate(angle);
      return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
Run Code Online (Sandbox Code Playgroud)

但是我们如何水平或垂直翻转图像?

java android image matrix android-bitmap

19
推荐指数
3
解决办法
1万
查看次数

如何检查Android上的位图是否为空(空白)

如何检查Bitmap对象是否完全空白,即所有像素都是透明的,每个像素上都没有xy循环?

android bitmap android-bitmap

18
推荐指数
1
解决办法
1万
查看次数

由java.lang.NullPointerException引起:尝试在空对象引用上调用虚方法'int android.graphics.Bitmap.getWidth()'

我有BitmapScalingHelper.java:

public class BitmapScalingHelper
{
    public static Bitmap decodeResource(Resources res, int resId, int dstWidth, int dstHeight)
    {
        Options options = new Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);
        options.inJustDecodeBounds = false;

        options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, dstWidth,
                dstHeight);

        Bitmap unscaledBitmap = BitmapFactory.decodeResource(res, resId, options);

        return unscaledBitmap;
    }

    public static Bitmap decodeFile(String filePath, int dstWidth, int dstHeight)
    {
        Options options = new Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        options.inJustDecodeBounds = false;
        options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, dstWidth,
                dstHeight);

        Bitmap unscaledBitmap = BitmapFactory.decodeFile(filePath, options); …
Run Code Online (Sandbox Code Playgroud)

android nullpointerexception android-activity android-bitmap

17
推荐指数
2
解决办法
2万
查看次数

从uri检索位图

我已经包含了"通过myApp共享"选项.我在接收活动类中插入了以下代码.

    // Get the intent that started this activity
    Intent intent = getIntent();
    Uri data = intent.getData();

    // Figure out what to do based on the intent type
    if (intent.getType().indexOf("image/") != -1) {
        // Handle intents with image data ...
}
Run Code Online (Sandbox Code Playgroud)

检索位图图像的下一步是什么?

android android-bitmap

15
推荐指数
2
解决办法
7万
查看次数

防止位图太大而无法上传到纹理android

我需要以图库形式全屏显示原始图像.对于拇指,它将完美地工作,当我尝试使用原始源以全屏显示该图像时,它将无法显示.在大多数情况下,如果图像分辨率大于2000,那么它将显示错误位图太大而无法上传到纹理android.

我想阻止这一点,我搜索谷歌但没有得到任何答案.

android android-bitmap

15
推荐指数
3
解决办法
3万
查看次数

ImageView.setImageBitmap()是否回收先前设置的位图?

假设我的代码类似于下面的代码:

protected void onCreate(Bundle bundle){

    this.imageView = (ImageView) contentView.findViewById(R.id.imageView);

    this.setFirstBitmap();
    this.setSecondBitmap();
}

private setFirstBitmap(){
    Bitmap bitmap1 = BitmapFactory.decodeFile(bitmapFile1);
    imageView.setImageBitmap(bitmap1);
}

private setSecondBitmap(){
    Bitmap bitmap2 = BitmapFactory.decodeFile(bitmapFile2);
    imageView.setImageBitmap(bitmap2);
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,imageView回收bitmap1还是我必须在设置bitmap2之前做到这一点?

android android-bitmap

14
推荐指数
2
解决办法
2万
查看次数

不是DRM文件,正常开放

我正在寻找一个关于通过调用BitmapFactory.decodeFile引起的重复日志打印的解决方案.

在我的应用程序中,我ListView每秒钟都会重新绘制一个计时器.在ListView具有ImageView该获取是来自本地存储的图像源,(而不是从网络)

图像存储在:

filePath = /data/data/com.xxx.testlib/files/b22a1a294fd6e5ad3ea3d25b63c4c735.jpg
Run Code Online (Sandbox Code Playgroud)

我使用以下代码重绘图像,它工作正常.没有例外.

try
{
 File filePath = context.getFileStreamPath(imageName);

 if(filePath.exists()){

    bMap = BitmapFactory.decodeFile(filePath.getPath());

 }

}catch (Exception e) 
{

 e.printStackTrace();

}
Run Code Online (Sandbox Code Playgroud)

但是在执行以下行时:

bMap = BitmapFactory.decodeFile(filePath.getPath());
Run Code Online (Sandbox Code Playgroud)

我在日志中得到如下打印:

03-07 09:55:29.100: I/System.out(32663): Not a DRM File, opening notmally
03-07 09:55:29.105: I/System.out(32663): buffer returned 
....
Run Code Online (Sandbox Code Playgroud)

如何从打印到日志中读取.

谢谢你

编辑

每当执行此操作时,它也会滞后于手机.这种降低的性能特别明显,特别是当手机正在Waked up使用此代码返回活动时.

它已超过一年的OP,但仍未找到答案.如果有人找到解决方案,请发布.

谢谢.

android imageview android-view android-bitmap

13
推荐指数
1
解决办法
5495
查看次数

在CustomView中连续绘制时性能不佳

使用案例:

我需要在我的视图上画出数百行和几段文字.我需要给出一个滚动效果,为此我捕获ACTION_MOVE事件并使用更新的点重绘所有行.为了得到欲望的结果,我尝试了不同的方法但没有按预期工作.

方法1

我做了一个扩展的自定义类View.所有绘图和计算都直接在我的onDraw()方法中完成.由于在onDraw()方法中完成了大量操作,因此应用程序的性能非常差.我甚至使用Profile GPU渲染检查了性能,我可以看到线条非常高.

方法2

我创建了一个Bitmap,在将所有行绘制到另一个线程中的位图后,我习惯postInvalidate()onDraw()方法中绘制位图:

mBufferedBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);   
mBufferedBitmap.eraseColor(Color.TRANSPARENT);    
Canvas mBufferedCanvas = new Canvas(mBufferedBitmap);               
drawLines(mBufferedCanvas)    
postInvalidate();
Run Code Online (Sandbox Code Playgroud)

由于我擦除了位图上的所有先前绘图并使用更新的点绘制新行,因此屏幕上会出现闪烁.

方法3

我尝试将自定义类扩展到SurfaceView另一个线程中的canvas对象上的所有操作.但由于SurfaceView使用CPU进行绘图操作,因此低配置手机的性能会很差.

谁能指导我如何以更好的性能完成这项任务?

performance android canvas surfaceview android-bitmap

13
推荐指数
1
解决办法
664
查看次数

错误:DisplayListCanvas.throwIfCannotDraw上的RuntimeException

我的应用程序在nougat模拟器和许多设备上工作得很好,但我在google play crash中发现了这个例外,我不知道为什么会发生这种情况,只有nougat设备才会导致异常.

例外:

   java.lang.RuntimeException: 
  at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:260)
  at android.graphics.Canvas.drawBitmap(Canvas.java:1420)
  at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:545)
  at android.view.View.getDrawableRenderNode(View.java:18591)
  at android.view.View.drawBackground(View.java:18527)
  at android.view.View.draw(View.java:18315)
  at android.view.View.updateDisplayListIfDirty(View.java:17302)
  at android.view.View.draw(View.java:18086)
  at android.view.ViewGroup.drawChild(ViewGroup.java:3966)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3752)
  at android.view.View.updateDisplayListIfDirty(View.java:17297)
  at android.view.View.draw(View.java:18086)
  at android.view.ViewGroup.drawChild(ViewGroup.java:3966)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3752)
  at android.view.View.updateDisplayListIfDirty(View.java:17297)
  at android.view.View.draw(View.java:18086)
  at android.view.ViewGroup.drawChild(ViewGroup.java:3966)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3752)
  at android.view.View.updateDisplayListIfDirty(View.java:17297)
  at android.view.View.draw(View.java:18086)
  at android.view.ViewGroup.drawChild(ViewGroup.java:3966)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3752)
  at android.view.View.updateDisplayListIfDirty(View.java:17297)
  at android.view.View.draw(View.java:18086)
  at android.view.ViewGroup.drawChild(ViewGroup.java:3966)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3752)
  at android.view.View.draw(View.java:18327)
  at com.android.internal.policy.DecorView.draw(DecorView.java:919)
  at android.view.View.updateDisplayListIfDirty(View.java:17302)
  at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:692)
  at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:698)
  at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:806)
  at android.view.ViewRootImpl.draw(ViewRootImpl.java:3135)
  at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2931)
  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2523)
  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1522)
  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7098)
  at …
Run Code Online (Sandbox Code Playgroud)

android android-canvas android-bitmap

13
推荐指数
3
解决办法
5473
查看次数