进度条和调色板

来源:互联网 发布:黄一琳网络直播的看法 编辑:程序博客网 时间:2024/06/05 05:09

首先是新建一个项目color。


然后在Layout下的xxActivity中布局,我的是MainActivity:

代码如下,我的布局有点儿奇葩,刚学,不怎么懂:

<TableLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" 
   >


 <TableRow>     
      <Button
          android:id="@+id/button1" 
             android:layout_width="35dp" 
             android:layout_height="38dp"              
               android:text="红"
               android:background="@drawable/btn"
              android:textColor="@color/red"
              />
      <TextView              
         android:layout_width="10dp" 
         android:layout_height="50dp"         
         android:text="" />
     <ProgressBar
         android:id="@+id/progress_bar1"
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content"
         android:layout_marginTop="13dp"        
         style="?android:attr/progressBarStyleHorizontal"
         android:max="255"/>         
     
</TableRow>    
      
 <TableRow>       
      <Button
          android:id="@+id/button2" 
             android:layout_width="40dp" 
               android:layout_height="38dp"              
               android:text="绿"
               android:background="@drawable/btn"
              android:textColor="#00ff00"
              />
      <TextView              
         android:layout_width="wrap_content" 
         android:layout_height="50dp"         
         android:text="" />
     <ProgressBar
         android:id="@+id/progress_bar2"
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
          android:layout_marginTop="13dp"
         style="?android:attr/progressBarStyleHorizontal"
         android:max="255"/> 
  </TableRow>
 
  <TableRow>        
     <Button
          android:id="@+id/button3" 
             android:layout_width="40dp" 
                android:layout_height="38dp"              
               android:text="蓝"
               android:background="@drawable/btn"
              android:textColor="@color/blue"
              />
            
      <TextView              
         android:layout_width="wrap_content" 
         android:layout_height="50px"         
         android:text="" />
   <ProgressBar
         android:id="@+id/progress_bar3"
         android:layout_width="350px" 
         android:layout_height="wrap_content" 
          android:layout_marginTop="13dp"
         style="?android:attr/progressBarStyleHorizontal"
         android:max="255"/> 
</TableRow> 
<LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"       
        android:orientation="vertical" >   


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"           
            android:orientation="horizontal" > 
            <TextView 
            android:id="@+id/textview1"             
            android:layout_width="50dp" 
            android:layout_height="50dp"         
            android:text="" /> 
          <SeekBar
            android:id="@+id/seek1"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            android:max="255"/> 
      </LinearLayout>
      
      <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"            
            android:orientation="horizontal" >             
        <TextView
            android:id="@+id/textview2"              
            android:layout_width="50dp" 
            android:layout_height="50dp"         
            android:text="" /> 
         <SeekBar
            android:id="@+id/seek2"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            android:max="255"/>
      </LinearLayout>
 
     <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"      
            android:orientation="horizontal" >               
        <TextView 
            android:id="@+id/textview3"             
            android:layout_width="50dp" 
            android:layout_height="50dp"         
            android:text="" />     
        <SeekBar
            android:id="@+id/seek3"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            android:max="255"/>
    </LinearLayout> 
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"       
            android:orientation="horizontal" >  
       <View
            android:id="@+id/view"
            android:layout_width="wrap_content"
            android:layout_height="350dp"
            >     
      </View>
   </LinearLayout>
</LinearLayout>
</TableLayout>


这个是必须的,拷贝到values下

然后就是src下的源码

代码入下:

package com.example.color;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;


public class MainActivity extends Activity implements OnClickListener,SeekBar.OnSeekBarChangeListener{
private Button button1,button2,button3;
private ProgressBar progress_bar1,progress_bar2,progress_bar3;

static int r = 0,g = 0 ,b = 0;//定义三个静态变量
private View view1; //声明view对象
private SeekBar seekbar1,seekbar2,seekbar3; //声明三个seekbar对象,进度条
     private TextView textview1,textview2,textview3;//右边的文本框
private View view;//最下面一个填充色框 
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button) findViewById(R.id.button1);
button2=(Button) findViewById(R.id.button2);
button3=(Button) findViewById(R.id.button3);
progress_bar1=(ProgressBar) findViewById(R.id.progress_bar1);
progress_bar2=(ProgressBar) findViewById(R.id.progress_bar2);
progress_bar3=(ProgressBar) findViewById(R.id.progress_bar3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
progress_bar1.setOnClickListener(this);
progress_bar2.setOnClickListener(this);
progress_bar3.setOnClickListener(this);



        view1 = (View)findViewById(R.id.view);//得到View对象        
        view1.setBackgroundColor(Color.rgb(r, g, b));//给view显示背景颜色
        
        seekbar1 = (SeekBar)findViewById(R.id.seek1);//得到三个seekbar对象
        seekbar2 = (SeekBar)findViewById(R.id.seek2);
        seekbar3 = (SeekBar)findViewById(R.id.seek3);       
        textview1=(TextView) findViewById(R.id.textview1); //获取textview对象
        textview2=(TextView) findViewById(R.id.textview2);
        textview3=(TextView) findViewById(R.id.textview3);
        view=(View) findViewById(R.id.view);//获取view对象
         
        seekbar1.setOnSeekBarChangeListener(this);//分别对三个seek绑定监听器  
        seekbar2.setOnSeekBarChangeListener(this);
        seekbar3.setOnSeekBarChangeListener(this);
}
public void onClick(View v){
switch(v.getId()){
case R.id.button1:
int progress1=progress_bar1.getProgress();
progress1=progress1+10;
progress_bar1.setProgress(progress1);
break;

case R.id.button2:
int progress2=progress_bar2.getProgress();
progress2=progress2+10;
progress_bar2.setProgress(progress2);
break;
   case R.id.button3:
  int progress3=progress_bar3.getProgress();
  progress3=progress3+10;//每点击一次加10
  progress_bar3.setProgress(progress3);
  break;
           }
}
//seekbar拖动时实现这个方法
    public void onProgressChanged(SeekBar seekbar,int arg1, boolean arg2){
    switch(seekbar.getId()){
    case R.id.seek1:
    r= seekbar1.getProgress();//获取进度
    textview1.setText(r+"");//给textview1设置值
    view.setBackgroundColor(Color.rgb(r, g, b));//设置view背景色
    break;
    case R.id.seek2:
    g= seekbar2.getProgress();
    textview2.setText(g+"");
    view.setBackgroundColor(Color.rgb(r, g, b));
    break;
    case R.id.seek3:
    b= seekbar3.getProgress();
    textview3.setText(b+"");
    view.setBackgroundColor(Color.rgb(r, g, b));
    break;
    }   
    }
   
    public  void onStartTrackingTouch (SeekBar seekbar){     
    }
    //停止拖动
    public void onStopTrackingTouch (SeekBar seekbar){  
    }
@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;
}

}



0 0
原创粉丝点击