简体   繁体   中英

Getting RGB effects with seek bars in android like the way in photoshop?

I am using three seek bars for red,green and blue to change an image color.When those three seek bars are at middle or extreme left or extreme right,the original image has to be displayed.I tried so much time to solve this.But i was unable to get it,please help me to solve this.

my main activity is

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;

public class Example extends Activity {

private int seekR, seekG, seekB,color;
SeekBar redSeekBar, greenSeekBar, blueSeekBar;
LinearLayout mScreen;
Bitmap source,destination,result;
ImageView changedImage;
Button show;
LinearLayout linear;
@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);
    show=(Button)findViewById(R.id.button1);
    changedImage=(ImageView)findViewById(R.id.changedImage);
    redSeekBar = (SeekBar)findViewById(R.id.mySeekingBar_R);
    greenSeekBar = (SeekBar)findViewById(R.id.mySeekingBar_G);
    blueSeekBar = (SeekBar)findViewById(R.id.mySeekingBar_B);
    linear=(LinearLayout)findViewById(R.id.linear);


    show.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(linear.getVisibility()==View.VISIBLE)
                linear.setVisibility(View.INVISIBLE);
            else
                linear.setVisibility(View.VISIBLE);

            updateBackground();
            redSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
            greenSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
                  blueSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);                
        }
    });   
}

private SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener()
{

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        // TODO Auto-generated method stub
        updateBackground();
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }


};

    private void updateBackground()
    {

    seekR = redSeekBar.getProgress();
    seekG = greenSeekBar.getProgress();
    seekB = blueSeekBar.getProgress();
    color=0xff000000+ seekR * 0x10000+ seekG * 0x100+ seekB;
    changeBitmapColor( color);
    }

   private void changeBitmapColor( int color) {


    destination = BitmapFactory.decodeResource(getResources(), R.drawable.myimage);
    result = destination.copy(Bitmap.Config.ARGB_8888, true);
    Paint p = new Paint();
    ColorFilter filter = new LightingColorFilter(color, 1);
    p.setColorFilter(filter);
    changedImage.setImageBitmap(result);
    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(result, 0, 0, p);
   }

 }

and my xml file is

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="106dp"
    android:text="Show" />

<ImageView
    android:id="@+id/changedImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/button1"
    android:layout_marginLeft="86dp"
    android:layout_marginTop="14dp"
    android:src="@drawable/myimage" />

   <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:orientation="vertical" 
    android:visibility="invisible"
    android:id="@+id/linear">

 <SeekBar 
  android:id="@+id/mySeekingBar_R" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:max="512" 
  android:progress="255" 
  android:paddingTop="10dp"/> 

 <SeekBar 
  android:id="@+id/mySeekingBar_G" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:max="512" 
  android:progress="255"
  android:paddingTop="10dp" />

 <SeekBar 
  android:id="@+id/mySeekingBar_B" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:max="512" 
  android:progress="255" 
  android:paddingTop="10dp"/>

 </LinearLayout>

</RelativeLayout>

Please help me how to do this.Thanks in advance.

I'm relative new to Android. But my idea (for testing purpose) is to change background using:

v.setBackgroundColor(0xFF00FF00);

v is the View, in the parameters of setBackroundColor you could insert generated values from seekbar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM