来自holocolorpicker的setTextColor和getcolor

时间:2014-08-13 19:51:22

标签: android

我正在使用holocolorpicker lib。我想获得颜色并将其设置为textview,但colorpicker返回无效颜色(返回颜色与我的颜色不同)。例如:

enter image description here

textview(masoud)上的文字与彩色圆圈有不同的颜色!

public class MainActivity extends ActionBarActivity implements OnColorChangedListener  {
    TextView txt ; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ColorPicker picker = (ColorPicker) findViewById(R.id.picker);
//      SVBar svBar = (SVBar) findViewById(R.id.svbar);
        OpacityBar opacityBar = (OpacityBar) findViewById(R.id.opacitybar);
        SaturationBar saturationBar = (SaturationBar) findViewById(R.id.saturationbar);
        ValueBar valueBar = (ValueBar) findViewById(R.id.valuebar);

//      picker.addSVBar(svBar);
        picker.addOpacityBar(opacityBar);
        picker.addSaturationBar(saturationBar);
        picker.addValueBar(valueBar);

        //To get the color
        picker.getColor();

        //To set the old selected color u can do it like this
        picker.setOldCenterColor(picker.getColor());
        // adds listener to the colorpicker which is implemented
        //in the activity
        picker.setOnColorChangedListener(this);

        //to turn of showing the old color
        picker.setShowOldCenterColor(false);

        //adding onChangeListeners to bars
//      opacitybar.setOnOpacityChangeListener(new OnOpacityChangeListener)
//      valuebar.setOnValueChangeListener(new OnValueChangeListener …)
//      saturationBar.setOnSaturationChangeListener(new OnSaturationChangeListener …)   

        txt = (TextView)findViewById(R.id.txt);
    }

    @Override
    public void onColorChanged(int color) {
        // TODO Auto-generated method stub

        final int c = color ;
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub

                txt.setTextColor(Color.rgb(c, c, c));
            }
        }, 2000);

    }

}

我的layout.xml:

<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.example.ex9_holocolorpicker.MainActivity" >

    <com.larswerkman.holocolorpicker.ColorPicker
        android:id="@+id/picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>


    <com.larswerkman.holocolorpicker.OpacityBar
        android:id="@+id/opacitybar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <com.larswerkman.holocolorpicker.SaturationBar
        android:id="@+id/saturationbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <com.larswerkman.holocolorpicker.ValueBar
        android:id="@+id/valuebar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>  

    <TextView 
        android:id="@+id/txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="masoud"
        />
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

Color.rgb(c,c,c) 表示您将c设置为RED GREEN和BLUE的值。

您可以使用Color.rgb(Color.red(c),Color.green(c),Color.blue(c));例如:)

编辑:更多http://developer.android.com/reference/android/graphics/Color.html

EDIT2:setTextColor(c)执行:p (诉到维克拉姆)