如何在 android 中设置 imageview.getDrawable

How to set imageview.getDrawable in android

我正在尝试使图像模糊,但我在 运行 应用程序上收到空点异常错误我开始知道它可能通过设置背景而不是 src 发生,所以我将其更改为src 但随后也得到同样的错误这里是我的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.union.test44.MainActivity"
tools:showIn="@layout/activity_main">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="@drawable/thumb5"/>

我的java代码是

Context context;
ImageView imageview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageview = (ImageView)findViewById(R.id.imageView);

BitmapDrawable drawable = (BitmapDrawable) imageview.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Bitmap blurred = blurRenderScript(bitmap, 24);//second parametre is radius
imageview.setImageBitmap(blurred);

}

@SuppressLint("NewApi")
private Bitmap blurRenderScript(Bitmap smallBitmap, int radius) {

try {
    smallBitmap = RGB565toARGB888(smallBitmap);
} catch (Exception e) {
    e.printStackTrace();
}


Bitmap bitmap = Bitmap.createBitmap(
        smallBitmap.getWidth(), smallBitmap.getHeight(),
        Bitmap.Config.ARGB_8888);

RenderScript renderScript = RenderScript.create(context);

Allocation blurInput = Allocation.createFromBitmap(renderScript, smallBitmap);
Allocation blurOutput = Allocation.createFromBitmap(renderScript, bitmap);

ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(renderScript,
        Element.U8_4(renderScript));
blur.setInput(blurInput);
blur.setRadius(radius); // radius must be 0 < r <= 25
blur.forEach(blurOutput);

blurOutput.copyTo(bitmap);
renderScript.destroy();

return bitmap;

}

private Bitmap RGB565toARGB888(Bitmap img) throws Exception {
int numPixels = img.getWidth() * img.getHeight();
int[] pixels = new int[numPixels];

//Get JPEG pixels.  Each int is the color values for one pixel.
img.getPixels(pixels, 0, img.getWidth(), 0, 0, img.getWidth(), img.getHeight());

//Create a Bitmap of the appropriate format.
Bitmap result = Bitmap.createBitmap(img.getWidth(), img.getHeight(), Bitmap.Config.ARGB_8888);

//Set RGB pixels.
result.setPixels(pixels, 0, result.getWidth(), 0, 0, result.getWidth(), result.getHeight());
return result;
}

最好直接使用:

// Your ImageView
ImageView originalImageView = (ImageView) findViewById(R.id.imageView);

// Input image (the drawable)
Bitmap inputImage = BitmapFactory.decodeResource(getResources(), R.drawable.thumb5);
int inputWidth = inputImage.getWidth();
int inputHeight = inputImage.getHeight();

Allocation inputAllocation = Allocation.createFromBitmap(mRS, inputImage);    

// Generate the output allocation
Type.Builder tbOutput = new Type.Builder(mRS, Element.RGBA_8888(mRS));
tbOutput.setX(inputWidth);
tbOutput.setY(inputHeight);
Allocation outputAllocation = Allocation.createTyped(mRS, tbOutput.create());

// Go on with the processing :)
...

// Then, set the bitmap
Bitmap outputBitmap = Bitmap.createBitmap(inputWidth, inputHeight, Bitmap.Config.ARGB_8888);

outputAllocation.copyTo(outputBitmap);

// Copy bitmap to ImageView    
originalImageView.setImageBitmap(outputBitmap);

参考:RenderScript: parallel computing on Android, the easy way

你试过吗:

Bitmap attachedbitmap= ((BitmapDrawable)imageview.getDrawable()).getBitmap();