目录
序言
由于自己最近由迷上了网页的制作,所以就一直在搞自己的网页http://whitebear.site/(,大家可以去我的网页看一下,提一下宝贵意见。)没有心思学习java。昨天灵机一动,想着得复习复习java了(就是想把自己想写但又懒得写的一个app给完成了),所以脑袋一热就把这个 选色器 给完成了。
代码zip,apk包
zip,apk包https://github.com/Gshihao/Android-https://github.com/Gshihao/Android-
写这篇文章纯粹是自己为了记录一下,里面的功能很简单,就是一跨自己用的工具。勿喷。
什么是颜色
既然我们想写一个选择器,里面肯定牵扯到颜色,颜色是由三原色(色光的三原色:红、绿、蓝,色料或颜料的三原色:黄、品红、青)组成的,三原色的色光以不同的比例相加,以产生多种多样的色光,即不同的颜色组成的。(自己查询的) 而这三色光就分别对应了三个值r(红色) g(绿色) b(蓝色),最后还有一个a表示透明度。
颜色的表示方式
颜色的表示方式有三种,
- 颜色的英文名称,如red红色等
- 16进制颜色代码如#RRGGBB 格式为 #AARRGGBB (AA可以不写)
- 10进制RGB码 如(RRR,GGG,BBB) 格式如此。
颜色的取值范围
16进制颜色码取值范围
16进制与10进制的数不同,16进制的数为0-9加a-f,具体内容可自行搜索。
RGB颜色吗的取值范围
RGB的取值数字为0-255,至于为什么是255,可以自己去搜索
代码结构
我们只要是通过界面上4个SeekBar来调节颜色的取值,把最大值设为255,每次滑动时(数值改变),我们可以通过调用Integer.toHexString(),来把10进制数来转换成16进制的数,并把他们组合起来,前面添加一个#,然后可以在界面上添加一个线性布局来展示颜色,每次一滑动就把组合后的颜色重新设置。
这里有一个小注意,就是大家可以把Integer.toHexString();单独放到一个方法里面,因为如果转换比10小的的数,他就会输出一位,我们需要单独在里面添加一个0
具体代码(仅供参考)
MainActivity.java
package com.mycompany.myappyanse;
import android.app.*;
import android.os.*;
import android.widget.*;
import android.content.*;
import android.graphics.*;
import android.content.ClipboardManager;
import android.view.*;
import android.net.*;
public class MainActivity extends Activity
{
private TextView seA;
private TextView seR;
private TextView seG;
private TextView seB;
private SeekBar sA;
private SeekBar sR;
private SeekBar sG;
private SeekBar sB;
private Context context;
private String zA="FF",zR="FF",zG="FF",zB="FF";
private int zaA=255,zaR=255,zaG=255,zaB=255;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context=MainActivity.this;
Localargb io=new Localargb();
Agetshu(); //A
Rgetshu();//R
Ggetshu();//G
Bgetshi();//B
meena();
}
@Override
public boolean onCreatePanelMenu(int featureId, Menu menu)
{
menu.add(1,1,1,"介绍");
menu.add(2,2,2,"更多");
menu.add(3,3,3,"退出");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// TODO: Implement this method
int id=item.getItemId();
switch(id){
case 1:
Intent intenta = new Intent();
intenta.setClass(MainActivity.this,Jieshao.class);
startActivity(intenta);
break;
case 2:
Uri uri = Uri.parse("http://whitebear.site/");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
//startActivity(new Intent(MainActivity.this,Jieshao.class));
break;
case 3:
this.finish();
}
return true;
}
private void meena()
{
//
}
public void jkl(View view){
//复制颜色 16进制
ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText("#"+zA+zR+zG+zB);
Toast.makeText(context,"已复制到剪切板",Toast.LENGTH_LONG).show();
}
public void rgba(View view ){
ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText(zaR+","+zaG+","+zaB+","+zaA);
Toast.makeText(context,"已复制到剪切板",Toast.LENGTH_LONG).show();
}
private void setback()
{
String zong="#"+zA+zR+zG+zB;
LinearLayout op=findViewById(R.id.mainLinearLayout1a);
TextView kl=findViewById(R.id.mainTextView1iokj);
kl.setText(""+zong);
op.setBackgroundColor(Color.parseColor(zong));
//op.setBackgroundColor(Color.parseColor(zong));
rgbys();
}
private void rgbys()
{
TextView as=findViewById(R.id.mainTextView1iokjyu);
as.setText("("+zaR+","+zaG+","+zaB+","+zaA+")");
}
private void Bgetshi()
{
// TODO: 获取B的值
sB=findViewById(R.id.mainSeekBar1B);
seB=findViewById(R.id.mainTextView1B);
sB.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar param1SeekBar, int param1Int, boolean param1Boolean)
{
// TODO: Implement this method
zB=Localargb.getA(param1Int);
zaB=param1Int;
seB.setText(Localargb.getA(param1Int));
setback();
}
@Override
public void onStartTrackingTouch(SeekBar param1SeekBar)
{
// TODO: Implement this method
}
@Override
public void onStopTrackingTouch(SeekBar param1SeekBar)
{
// TODO: Implement this method
}
});
}
private void Ggetshu()
{
// TODO: 获取G的值
sG=findViewById(R.id.mainSeekBar1G);
seG=findViewById(R.id.mainTextView1G);
sG.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar param1SeekBar, int param1Int, boolean param1Boolean)
{
// TODO: Implement this method
zaG=param1Int;
zG=Localargb.getA(param1Int);
seG.setText(Localargb.getA(param1Int));
setback();
}
@Override
public void onStartTrackingTouch(SeekBar param1SeekBar)
{
// TODO: Implement this method
}
@Override
public void onStopTrackingTouch(SeekBar param1SeekBar)
{
// TODO: Implement this method
}
});
}
private void Rgetshu()
{
// TODO: 获取R的值
sR=findViewById(R.id.mainSeekBar1R);
seR=findViewById(R.id.mainTextView1R);
sR.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar param1SeekBar, int param1Int, boolean param1Boolean)
{
// TODO: Implement this method
zaR=param1Int;
zR=Localargb.getA(param1Int);
seR.setText(Localargb.getA(param1Int));
setback();
}
@Override
public void onStartTrackingTouch(SeekBar param1SeekBar)
{
// TODO: Implement this method
}
@Override
public void onStopTrackingTouch(SeekBar param1SeekBar)
{
// TODO: Implement this method
}
});
}
private void Agetshu()
{
//获取A的值
sA=findViewById(R.id.mainSeekBar1A);
seA=findViewById(R.id.mainTextView1a);
sA.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar param1SeekBar, int param1Int, boolean param1Boolean)
{
// TODO: Implement this method
zaA=param1Int;
zA=Localargb.getA(param1Int);
seA.setText(Localargb.getA(param1Int));
setback();
}
@Override
public void onStartTrackingTouch(SeekBar param1SeekBar)
{
//点击时
}
@Override
public void onStopTrackingTouch(SeekBar param1SeekBar)
{
// TODO: Implement this method
}
});
}
}
可能自己写的比较乱,但是流程就是那么个流程
Localargb.java
package com.mycompany.myappyanse;
import java.util.*;
public class Localargb
{
public static String getA(int a){
if(a <16 ){
return "0"+Integer.toHexString(a);
}
return Integer.toHexString(a);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|center"
android:id="@+id/mainTextView1jsj">
<LinearLayout
android:orientation="horizontal"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="100dp"
android:background="#FF2DDCA7"
android:id="@+id/mainLinearLayout1a">
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="25dp"
android:padding="10dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="16进制:"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mainTextView1iokj"
android:onClick="jkl"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RGB:"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mainTextView1iokjyu"
android:onClick="rgba"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注:点击颜色值即可复制"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginTop="10dp"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:gravity="center"
android:padding="10dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A:"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/mainTextView1a"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainSeekBar1A"
android:max="255"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="R:"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/mainTextView1R"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainSeekBar1R"
android:max="255"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="G:"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/mainTextView1G"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainSeekBar1G"
android:max="255"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B:"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/mainTextView1B"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainSeekBar1B"
android:max="255"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="白熊出品"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>