Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android程序化模糊imageview drawable大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在 Android中以编程方式模糊和清除图像.

我听说API 14之后不再支持Android标志“模糊”,但是我也想使用Java方法.我的主要问题是从Imageview drawable操作位图.

如何从图像视图中获取位图并进行操作(可能会使用高斯模糊)并将其设置回到图像视图?我认为这个过程涉及提取drawable,将drawable转换为位图,在该位图上执行我的模糊方法,然后进行相反操作,直到再次设置为imageview

但是我希望这个过程很清楚,谢谢

解决方法

以下是实现高斯模糊的代码.可能这可以帮助你
import android.graphics.bitmap;
import android.graphics.Matrix;

/**
 * @author robert.hinds
 *
 * Wrapper class for the Android Bitmap - used by all filters
 *
 */
public class AndroidImage {

    //original bitmap image
    private Bitmap image;

    //format of image (jpg/png)
    private String formatName;

    //dimensions of image
    privatE int width,height;

    // RGB Array Color
    protected int[] colourArray;

    public AndroidImage(Bitmap img){        
        this.image =  img;
        formatName = "jpg";
        width = img.getWidth();
        height = img.getHeight();
        updateColourArray();
    }


    /**
     * Method to reset the image to a solid colour
     * 
     * @param color - colour to rest the entire image to
     */
    public void clearImage(int color){
        for(int y=0; y<height; y++){
            for(int x=0; x<width; x++){
                image.setPixel(x,y,color);
            }
        }
    }


    /**
     * Set colour array for image - called on initialisation
     * by constructor
     * 
     * @param bitmap
     */
    private void updateColourArray(){
        colourArray = new int[width * height];
        image.getPixels(colourArray,width,height);
        int r,g,b;
        for (int y = 0; y < height; y++){
            for (int x = 0; x < width; x++){
                int index = y * width + x;
                r = (colourArraY[index] >> 16) & 0xff;
                g = (colourArraY[index] >> 8) & 0xff;
                b = colourArraY[index] & 0xff;
                colourArraY[index] = 0xff000000 | (r << 16) | (g << 8) | b;
            }
        }
    }


    /**
     * Method to set the colour of a specific pixel
     * 
     * @param x
     * @param y
     * @param colour
     */
    public void setPixelColour(int x,int y,int colour){
        colourArraY[((y*image.getWidth()+X))] = colour;
        image.setPixel(x,colour);
    }

    /**
     * Get the colour for a specified pixel
     * 
     * @param x
     * @param y
     * @return colour
     */
    public int getPixelColour(int x,int y){
        return colourArraY[y*width+x];
    }

    /**
     * Set the colour of a specified pixel from an RGB combo
     * 
     * @param x
     * @param y
     * @param c0
     * @param c1
     * @param c2
     */
    public void setPixelColour(int x,int c0,int c1,int c2){
        colourArraY[((y*image.getWidth()+X))] = (255 << 24) + (c0 << 16) + (c1 << 8) + c2;
        image.setPixel(x,colourArraY[((y*image.getWidth()+X))]);
    }

    /**
     * Method to get the RED colour for the specified 
     * pixel 
     * @param x
     * @param y
     * @return colour of R
     */
    public int getRComponent(int x,int y){
        return (getColourArray()[((y*width+X))]& 0x00FF0000) >>> 16;
    }


    /**
     * Method to get the GREEN colour for the specified 
     * pixel 
     * @param x
     * @param y
     * @return colour of G
     */
    public int getGComponent(int x,int y){
        return (getColourArray()[((y*width+X))]& 0x0000FF00) >>> 8;
    }


    /**
     * Method to get the BLUE colour for the specified 
     * pixel 
     * @param x
     * @param y
     * @return colour of B
     */
    public int getBComponent(int x,int y){
        return (getColourArray()[((y*width+X))] & 0x000000FF);
    }



    /**
     * Method to rotate an image by the specified number of degrees
     * 
     * @param rotatedegrees
     */
    public void rotate (int rotatedegrees){
        Matrix mtx = new Matrix();
        mtx.postRotate(rotatedegrees);
        image = Bitmap.createBitmap(image,height,mtx,truE);
        width = image.getWidth();
        height = image.getHeight();
        updateColourArray();
    }


    /**
     * @return the image
     */
    public Bitmap getImage() {
        return image;
    }


    /**
     * @param image the image to set
     */
    public void setImage(Bitmap imagE) {
        this.image = image;
    }


    /**
     * @return the formatName
     */
    public String getFormatName() {
        return formatName;
    }


    /**
     * @param formatName the formatName to set
     */
    public void setFormatName(String formatName) {
        this.formatName = formatName;
    }


    /**
     * @return the width
     */
    public int getWidth() {
        return width;
    }


    /**
     * @param width the width to set
     */
    public void setWidth(int width) {
        this.width = width;
    }


    /**
     * @return the height
     */
    public int getHeight() {
        return height;
    }


    /**
     * @param height the height to set
     */
    public void setHeight(int height) {
        this.height = height;
    }


    /**
     * @return the colourArray
     */
    public int[] getColourArray() {
        return colourArray;
    }


    /**
     * @param colourArray the colourArray to set
     */
    public void setColourArray(int[] colourArray) {
        this.colourArray = colourArray;
    }

}
import com.bvise.fotoflipper.core.AndroidImage;




public interface IAndroidFilter {

    public AndroidImage process(AndroidImage imageIn);
}


import android.graphics.bitmap;
import android.graphics.Color;

public class ConvolutionMatrix
{
    public static final int SIZE = 3;

    public double[][] Matrix;
    public double Factor = 1;
    public double Offset = 1;

    public ConvolutionMatrix(int sizE) {
        Matrix = new double[size][size];
    }

    public void setAll(double value) {
        for (int x = 0; x < SIZE; ++X) {
            for (int y = 0; y < SIZE; ++y) {
                Matrix[x][y] = value;
            }
        }
    }

    public void applyConfig(double[][] config) {
        for(int x = 0; x < SIZE; ++X) {
            for(int y = 0; y < SIZE; ++y) {
                Matrix[x][y] = config[x][y];
            }
        }
    }

    public static Bitmap computeConvolution3x3(Bitmap src,ConvolutionMatrix matriX) {
        int width = src.getWidth();
        int height = src.getHeight();
        Bitmap result = Bitmap.createBitmap(width,src.getConfig());

        int a,R,G,B;
        int sumR,sumG,sumB;
        int[][] pixels = new int[SIZE][SIZE];

        for(int y = 0; y < height - 2; ++y) {
            for(int x = 0; x < width - 2; ++X) {

                // get pixel matrix
                for(int i = 0; i < SIZE; ++i) {
                    for(int j = 0; j < SIZE; ++j) {
                        pixels[i][j] = src.getPixel(x + i,y + j);
                    }
                }

                // get alpha of center pixel
                A = Color.alpha(pixels[1][1]);

                // init color sum
                sumR = sumG = sumB = 0;

                // get sum of RGB on matrix
                for(int i = 0; i < SIZE; ++i) {
                    for(int j = 0; j < SIZE; ++j) {
                        sumR += (Color.red(pixels[i][j]) * matrix.Matrix[i][j]);
                        sumG += (Color.green(pixels[i][j]) * matrix.Matrix[i][j]);
                        sumB += (Color.blue(pixels[i][j]) * matrix.Matrix[i][j]);
                    }
                }

                // get final Red
                R = (int)(sumR / matrix.Factor + matrix.offset);
                if(R < 0) { R = 0; }
                else if(R > 255) { R = 255; }

                // get final Green
                G = (int)(sumG / matrix.Factor + matrix.offset);
                if(G < 0) { G = 0; }
                else if(G > 255) { G = 255; }

                // get final Blue
                B = (int)(sumB / matrix.Factor + matrix.offset);
                if(B < 0) { B = 0; }
                else if(B > 255) { B = 255; }

                // apply new pixel
                result.setPixel(x + 1,y + 1,Color.argb(A,B));
            }
        }

        // final image
        return result;
    }
}
import android.graphics.bitmap;

import com.bvise.fotoflipper.core.AndroidImage;
import com.bvise.fotoflipper.core.ConvolutionMatrix;
import com.bvise.fotoflipper.filters.IAndroidFilter;

public class GaussianBlur implements IAndroidFilter{

    @Override
    public AndroidImage process(AndroidImage imageIn) {
        // TODO Auto-generated method stub
        Bitmap src=imageIn.getImage();
        double[][] GaussianBlurConfig = new double[][] {
                { 1,2,1 },{ 2,4,2 },{ 1,1 }
            };
            ConvolutionMatrix convMatrix = new ConvolutionMatrix(3);
            convMatrix.applyConfig(GaussianBlurConfig);
            convMatrix.Factor = 200;
            convMatrix.offset = 0;
            return new AndroidImage(ConvolutionMatrix.computeConvolution3x3(src,convMatriX));
    }


}

大佬总结

以上是大佬教程为你收集整理的android程序化模糊imageview drawable全部内容,希望文章能够帮你解决android程序化模糊imageview drawable所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。