【问题标题】:How to change pixel color for bitmap?如何更改位图的像素颜色?
【发布时间】:2014-11-07 11:41:39
【问题描述】:

我有一个位图对象。当我将 getpixel() 用于位图并在 setPixel 用于该位图对象之后,像素值(颜色)不会改变。我使用 imageview 在背景图像上填充颜色。主要问题是我不能为图像视图的同一区域填充颜色。当我在 imageview 上填充颜色时,这些区域不用于填充另一种颜色。意味着我不更新 imageview 的填充颜色区域。以下是我的源代码:

public static int DARK_PINK = Color.argb(255, 255, 51, 255);
    public static int LIGHT_YELLOW = Color.argb(255, 255, 230, 102);
    public static int DARK_MARUN = Color.argb(255, 148, 66, 50);
    public static int LIGHT_MARUN = Color.argb(255, 186, 123, 68);
    public static int RED = Color.argb(255, 252, 20, 20);
    public static int LIGHT_BLUE = Color.argb(255, 102, 255, 255);
    public static int DARK_BLUE = Color.argb(255, 70, 78, 202);
    public static int LIGHT_GREEN = Color.argb(255, 190, 255, 91);
    public static int DARK_GREEN = Color.argb(255, 15, 230, 0);
    public static int JAMBLI = Color.argb(255, 123, 0, 230);
    public static int ORANGE = Color.argb(255, 255, 187, 50);
    public static int BLACK = Color.argb(255, 7, 5, 0);
    public static int GRAY = Color.argb(255, 129, 128, 127);
    public static int PINK_RED = Color.argb(255, 255, 4, 139);
    public static int NEAVYBLUE = Color.argb(255, 51, 204, 255);
    public static int ADVANCE_GREEN = Color.argb(255, 102, 255, 204);

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.activity_main);

Bundle extras = getIntent().getExtras();
        byte[] byteArray = extras.getByteArray("picture");

        bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);


        bitmap = bmp.copy(Bitmap.Config.ARGB_8888, true);
        canvas = new Canvas(bitmap);
        paint = new Paint();
        paint.setColor(DARK_PINK);
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStrokeWidth(30);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER));
        // paint = new Paint(Paint.DITHER_FLAG);
        paths.add(path);
        path = new Path();

        image.setImageBitmap(bitmap);

        image.setOnTouchListener(this);


@Override
    public void onClick(View v) {
    switch (v.getId()) {

        case R.id.btn_color_one: {
    int pixel = DARK_PINK;

                bitmap.setPixel(x, y, pixel);
                if(pixel==DARK_PINK){
                    pixel = LIGHT_YELLOW;
                    bitmap.setPixel(x, y, pixel);
                    canvas = new Canvas(bitmap);

                }

                else{

                    Toast.makeText(getApplicationContext(), "Not Changed", 2).show();

                }
}
}
});
}

【问题讨论】:

    标签: android bitmap pixel


    【解决方案1】:

    尝试给它一个像这样的 RGB 颜色编号

    myBitmap.setPixel(x, y, Color.rgb(45, 127, 0));
    

    【讨论】:

    • 我无法更改前一个像素的颜色,我不知道该怎么做。因为我尝试了很多方法来改变像素值但没有成功。
    • 我添加了整个代码逻辑和图像以供理解。
    猜你喜欢
    • 1970-01-01
    • 2020-10-10
    • 2011-08-20
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多