Android色调图像有2种不同的颜色

时间:2015-02-25 06:14:58

标签: android colors bitmap tint

我试图将图像左半部分用橙色着色,右边用栗色。我写了我的代码,但每当我尝试时,它只返回纯橙色和栗色。所以这就是我想要做的。

我想用左手和右手用橙色和栗色调好量,这样就好像

Success

This is my image that is failed

像这样,我的工作不像示例。这是我的代码。

public Bitmap toHokie(Bitmap bmpOriginal) {
    int width, height;

    Bitmap bmOut = Bitmap.createBitmap(bmpOriginal.getWidth(),
            bmpOriginal.getHeight(), bmpOriginal.getConfig());
    height = bmOut.getHeight();
    width = bmOut.getWidth();
    int orangeFilter = new Color().rgb(255, 165, 0);
    int maroonFilter = new Color().rgb(139, 0, 0);
    for (int j = 0; j < height - 1; j++) {
        for (int i = 0; i < width / 2 - 1; i++) {
            int newColor = (int) ((double) (bmOut.getPixel(i, j) * 0.3) + ((double) (orangeFilter * 0.7)));

            bmOut.setPixel(i, j, newColor);
        }
    }

    for (int j = 0; j < height - 1; j++) {
        for (int i = width / 2; i < width - 1; i++) {
            double newColor = (bmOut.getPixel(i, j) * 0.3 + maroonFilter * 0.7);
            bmOut.setPixel(i, j, (int) newColor);
        }
    }
    return bmOut;
}

实际上,对于我的第二次尝试,它现在比以前更好了,但它仍然是有线的...像这样

I guess something is working....

我这样修好了。

    public Bitmap toHokie(Bitmap bmpOriginal) {
    int width, height;

    Bitmap bmOut = Bitmap.createBitmap(bmpOriginal.getWidth(),
            bmpOriginal.getHeight(), bmpOriginal.getConfig());
    height = bmOut.getHeight();
    width = bmOut.getWidth();
    int orangeFilter = new Color().rgb(255, 165, 0);
    int maroonFilter = new Color().rgb(139, 0, 0);
    for (int j = 0; j < height - 1; j++) {
        for (int i = 0; i < width / 2 - 1; i++) {
            int newColor = (int) ((bmpOriginal.getPixel(i, j) * 0.7) + ((orangeFilter * 0.3)));

            bmOut.setPixel(i, j, newColor);
        }
    }

    for (int j = 0; j < height - 1; j++) {
        for (int i = width / 2; i < width - 1; i++) {
            double newColor = (bmpOriginal.getPixel(i, j) * 0.3 + maroonFilter * 0.7);
            bmOut.setPixel(i, j, (int) newColor);
        }
    }
    return bmOut;
}

1 个答案:

答案 0 :(得分:0)

这是xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="320dp"
    android:layout_height="320dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:src="@drawable/sachin_bg1" />

<LinearLayout
    android:layout_width="320dp"
    android:layout_height="320dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:orientation="horizontal"
    android:weightSum="1" >

    <LinearLayout
        android:id="@+id/layout1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="#4a8cd5"
        android:orientation="horizontal" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="top"
        android:layout_weight="0.5"
        android:background="#f21616"
        android:orientation="horizontal" >
    </LinearLayout>
</LinearLayout>

在onCreate方法中使用此代码

LinearLayout layout1 = (LinearLayout) findViewById(R.id.layout1);
    Drawable background1 = layout1.getBackground();
    background1.setAlpha(100);

    LinearLayout layout2 = (LinearLayout) findViewById(R.id.layout2);
    Drawable background2 = layout2.getBackground();
    background2.setAlpha(100);