Android中的调色板功能的实现

本文介绍了在Android中实现调色板功能的详细步骤,包括在XML布局文件中声明进度条和Textview控件,以及在MainActivity中设置监听器以根据进度条数值改变颜色并显示十六进制值。此外,还提到了可以通过添加透明度进度条进一步扩展功能,并预告了下篇博客将讲解如何自定义漂亮的标题栏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现调色板的详细步骤

这是实现后的效果图片1、 大体步骤: 1) 第一步在activity_main.xml中声明三个进度条(seekBar)和三个表示颜色的Textview控件。大容器用的布局是Linelayout,然后将显示颜色字体的textview和seekbar嵌套Linelayout布局。 2) 第二步:在mainactivity.java中设置监听器(OnSeekBarChangeListener),实现它的三个未实现的方法,在方法里面获取当前进度条的数值,用设置背景颜色的方法显示颜色,再用一个封装的将数值转化成十六进制的方法将其显示在textview中。2、 详细步骤: 1. 在activitity_main.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.test.colorchanged.MainActivity" >

  <com.test.colorchanged.titlelayout 
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      ></com.test.colorchanged.titlelayout>

    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

    <TextView
        android:id="@+id/txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="红色" />

    <SeekBar
        android:id="@+id/seekbar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:max="255"
        android:progress="0" />


    </LinearLayout>

    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <TextView
        android:id="@+id/txt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="绿色" />
    <SeekBar
        android:id="@+id/seekbar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="20dp"
        android:max="255"
        android:progress="0" />

    </LinearLayout>



    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    <TextView
        android:id="@+id/txt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="蓝色" />

    <SeekBar
        android:id="@+id/seekbar3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="20dp"
        android:max="255"
        android:progress="0" />

    </LinearLayout>
<RelativeLayout 
    android:layout_width="fill_parent"
    android:orientation="vertical"
        android:layout_height="fill_parent"
    >
    <TextView 
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"       
        />
     <EditText
        android:id="@+id/edtxt"
        android:layout_width="fill_parent"
        android:layout_below="@+id/text1"
        android:layout_height="fill_parent"
        />

</RelativeLayout>

</LinearLayout>

2、在Mainactivity中添加seekbar的监听类:

package com.test.colorchanged;

import org.w3c.dom.Text;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {

    private SeekBar seekbar1,seekbar2,seekbar3;
    private EditText edtxt;
    private TextView txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        seekbar1 =
                (SeekBar) findViewById(R.id.seekbar1);
        seekbar2 =(SeekBar) findViewById(R.id.seekbar2);
        seekbar3 =(SeekBar) findViewById(R.id.seekbar3);
        edtxt=(EditText) findViewById(R.id.edtxt);
        txt=(TextView) findViewById(R.id.text1);

        seekbar1.setOnSeekBarChangeListener(new listener());
        seekbar2.setOnSeekBarChangeListener(new listener());
        seekbar3.setOnSeekBarChangeListener(new listener());


    }
    class listener implements OnSeekBarChangeListener{


        //进度条改变

        public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
            // TODO Auto-generated method stub
            int progress1=seekbar1.getProgress();
            int progress2=seekbar2.getProgress();
            int progress3=seekbar3.getProgress();
            edtxt.setBackgroundColor(Color.rgb(progress1, progress2, progress3));

            txt.setText(Integer.toHexString(progress1)+Integer.toHexString(progress2)+Integer.toHexString(progress3));

        }
        @Override
        //开始拖动
        public void onStartTrackingTouch(SeekBar arg0) {
            // TODO Auto-generated method stub
            int progress1=seekbar1.getProgress();
            int progress2=seekbar2.getProgress();
            int progress3=seekbar3.getProgress();

            edtxt.setBackgroundColor(Color.rgb(progress1, progress2, progress3));

            txt.setText(Integer.toHexString(progress1)+Integer.toHexString(progress2)+Integer.toHexString(progress3));
        }

        @Override
        //停止拖动
        public void onStopTrackingTouch(SeekBar arg0) {
            // TODO Auto-generated method stub
            int progress1=seekbar1.getProgress();
            int progress2=seekbar2.getProgress();
            int progress3=seekbar3.getProgress();
            edtxt.setBackgroundColor(Color.rgb(progress1, progress2, progress3));
            txt.setText(Integer.toHexString(progress1)+Integer.toHexString(progress2)+Integer.toHexString(progress3));

        }

    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

好的,现在就已经实现了三个调色板的功效了,还可以有一个透明度的seekbar,这里小编就没有写啦,还是很简单的,不知道有没有细心的读者注意到,你们写出来的揭界面跟小编的不太一样呐,,对的,就是我们默认的标题栏不太一样,我这里用了一个自定义的控件。好的,小编的下一篇博客会讲怎样自己这只一个漂亮的标题栏而不哟个默认的标题栏的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值