实现调色板的详细步骤
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,这里小编就没有写啦,还是很简单的,不知道有没有细心的读者注意到,你们写出来的揭界面跟小编的不太一样呐,,对的,就是我们默认的标题栏不太一样,我这里用了一个自定义的控件。好的,小编的下一篇博客会讲怎样自己这只一个漂亮的标题栏而不哟个默认的标题栏的。