Android制作简易的调色器,并实现复制色值的功能
我们上课老师让做的作业,参照别人的代码,可能不够完善,请大家见谅…
主要用到SeekBar控件
先展示效果图吧
点击复制的Button,弹出提示信息,复制成功,并且可以粘贴。
设置button样式:定义了一个style.xml文件
<resources>
<style name="Button_List_Style" parent="@style/Widget.AppCompat.Button.Borderless">
<!-- 去除button投影效果-->
</style>
</resources>
这里我自定义一个布局文件 footer.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:weightSum="10"
android:layout_weight="2.5"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:background="@mipmap/footer1"
style="@style/Button_List_Style"/>
<View
android:layout_width="0dp"
android:background="#ffffff"
android:layout_weight="2"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:background="@mipmap/footer2"
style="@style/Button_List_Style"/>
</LinearLayout>
修改activity_main.xml布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="0.5"
android:text="R:" />
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="10"
android:layout_marginTop="12dp"
android:max="255"
android:padding="10px"
android:progress="00" />
<EditText
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="00"
android:ems="5" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="0.5"
android:text="G" />
<SeekBar
android:id="@+id/seekBar2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:max="255"
android:progress="0"
android:layout_weight="10" />
<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="00"
android:ems="5" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="0.5"
android:text="B" />
<SeekBar
android:id="@+id/seekBar3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:max="255"
android:progress="0"
android:layout_weight="10" />
<EditText
android:id="@+id/editText3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="00"
android:ems="5" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:textSize="20dp"
android:hint="当前值是:#000000"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:gravity="center"
android:onClick="onClickCopy"
android:text="复制"
/>
</LinearLayout>
<EditText
android:id="@+id/textView5"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_below="@+id/textView1"
android:background="#000000"
android:layout_gravity="center_horizontal"
/>
<TextView
android:id="@+id/txt4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="30dp"
android:text="分享到"
android:textSize="15dp"
android:layout_gravity="center"
android:gravity="center"
/>
<include layout="@layout/footer" />
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="版权归**所有"/>
</LinearLayout>
修改MainActivity.java
public class MainActivity extends AppCompatActivity{
private TextView textView;
private SeekBar seekBar1;
private SeekBar seekBar2;
private SeekBar seekBar3;
private EditText editText1;
private EditText editText2;
private EditText editText3;
private String sTmp="000000";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView1);
seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
seekBar2 = (SeekBar) findViewById(R.id.seekBar2);
seekBar3 = (SeekBar) findViewById(R.id.seekBar3);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
String str = String.format("%1$02x", progress);
editText1.setText(str);
doWork();
}
});
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
// TODO Auto-generated method stub
editText2.setText(String.format("%1$02x", progress));
doWork();
}
});
seekBar3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
// TODO Auto-generated method stub
editText3.setText(String.format("%1$02x", progress));
doWork();
}
});
}
private void doWork() {
sTmp = editText1.getText().toString() + editText2.getText().toString() + editText3.getText().toString();
textView.setText("当前值是:#" + sTmp);
TextView tView = (TextView) findViewById(R.id.textView5);
tView.setBackgroundColor(Color.parseColor("#" + sTmp));
}
public void onClickCopy(View v) {
// 从API11开始android推荐使用android.content.ClipboardManager
// 为了兼容低版本我们这里使用旧版的android.text.ClipboardManager,虽然提示deprecated,但不影响使用。
ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
// 将文本内容放到系统剪贴板里。
cm.setText("#"+sTmp);
Toast.makeText(this, "复制成功,可以分享给朋友们了。", Toast.LENGTH_LONG).show();
}