【问题标题】:android changing color of image from left to right smoothlyandroid从左到右平滑地改变图像的颜色
【发布时间】:2017-01-07 13:36:11
【问题描述】:

我想像光谱一样从左到右改变图像的颜色,并看到颜色的变化,这是我的代码,问题是它从原始图像变为最终图像后单击按钮,但我不知道如何更改它,因此我可以看到更改的变化。

final ImageView imgView = (ImageView) findViewById(R.id.imageView);
    Button btn = (Button) findViewById(R.id.button1);

    Bitmap bitmap = ((BitmapDrawable) imgView.getDrawable()).getBitmap();
    final Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {


            for( x = 0; x < mutableBitmap.getWidth() ; x++){
                runOnUiThread (new Thread(new Runnable() { 
                     public void run() {
                         try {
                                        for(int y = 0; y < mutableBitmap.getHeight() ; y++){
                                            int c = mutableBitmap.getPixel(x, y);
                                            mutableBitmap.setPixel(x, y, Color.rgb(Color.red(c), (200 - Color.green(c)), Color.blue(c)));
                                            Log.d("IMAGE", ""+x);
                                        }   
                                        imgView.setImageBitmap(mutableBitmap);
                                            Thread.sleep(100);
                                        } catch (InterruptedException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }
                             }

                }));
            }
        }


    });

【问题讨论】:

  • 你的意思是像color gradient吗?
  • 是的,例如它以白色开始并以绿色连续结束。
  • 考虑使用GradientDrawable,而不是组装自己的位图。
  • 是的,但问题是我希望颜色从左到右移动,就像刷一个电话一样。实际上我应该这样做是为了在手机上滑动接听电话,例如三星手机上常见的事情。

标签: android bitmap spectrum


【解决方案1】:

您可以右键单击您的drawable 文件夹,选择New\Drawable 资源文件。为文件选择一个名称,然后单击“确定”。 之后,您可以添加以下代码:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:angle="135"
    android:centerColor="@color/colorAccent"
    android:endColor="@color/colorPrimaryDark"
    android:startColor="@color/colorPrimary"
    android:type="linear" />

您可以从 color.xml 创建新颜色并使用它们。然后在java代码中:

imageView.setImageDrawable(getResources().getDrawable(R.drawable.YOURDRAWABLENAME));

【讨论】:

    【解决方案2】:

    我不是专家,但我认为你可以试试这个来选择你想要的颜色:

    int[] colors = {0xFFFFFFFF, 0xFFFF0000, 0xFF00FF00}
    

    然后应用它们:

    mutableBitmap.setPixel(x, y, new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, colors);
    

    希望对你有帮助

    【讨论】:

    • 谢谢,但是“Bitmap 类型中的 setPixel(int, int, int) 方法不适用于参数 (int, int, GradientDrawable)”
    • 必须通过代码来实现吗?因为使用 xml 很容易。您创建一个新的可绘制对象并将其应用于 ImageView,如果您有兴趣并且这是您想要的,我可以告诉您如何
    • @BitaMirshafiee
    • 我正在尝试通过代码来做,如果你能告诉我xml实践,我将不胜感激
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多