android简单调色画板
2016-05-08 14:18
447 查看
这次是基于调色板上改的画板,上面的---------1.---------------2.------------------3.-------------------4是相互对应的,方便看主要代码:
(MainActivity.java)
package com.example.color;
import java.io.File;
import java.io.FileOutputStream;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener,SeekBar.OnSeekBarChangeListener,OnTouchListener{
private Button button1,button2,button3;//红绿蓝
private ProgressBar progress_bar1,progress_bar2,progress_bar3;//缓冲条
//缓冲条----------------------------------------------------------------------------------------1
static int r = 0,g = 0 ,b = 0;//定义三个静态变量
private SeekBar seekbar1,seekbar2,seekbar3; //声明三个,进度条seekbar对象
private TextView textview1,textview2,textview3;//左边的显示进度数字的文本框
private View view;//最下面一个填充色框
//进度条----------------------------------------------------------------------------------------2
private Button red,green,blue;//按钮红绿蓝
//按钮---------------------------------------------------------------------------------------- 3
//第一步:声明一些全局上使用的变量
//FrameLayout上面一层
private ImageView imageview;//接收布局文件上的显示控件 ,让画布在上面画
private Bitmap baseBitmap;//声明一个可以用来编辑的bitmap(位图),相当于图片的存储空间
private Canvas canvas; //画布的声明
private Paint paint; //画笔的声明
private float startx;//设置点击的坐标点x
private float starty;//设置点击的坐标点y
//画笔---------------------------------------------------------------------------------------- 4
@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);
//----------------------------------------------------------------------------------------- 1
textview1=(TextView) findViewById(R.id.textview1); //获取textview对象
textview2=(TextView) findViewById(R.id.textview2);
textview3=(TextView) findViewById(R.id.textview3);
seekbar1 = (SeekBar)findViewById(R.id.seek1);//得到三个seekbar对象
seekbar2 = (SeekBar)findViewById(R.id.seek2);
seekbar3 = (SeekBar)findViewById(R.id.seek3);
view=(View) findViewById(R.id.view);//获取view对象
seekbar1.setOnSeekBarChangeListener(this);//分别对三个seek绑定监听器
seekbar2.setOnSeekBarChangeListener(this);
seekbar3.setOnSeekBarChangeListener(this);
//------------------------------------------------------------------------------------------2
red=(Button) findViewById(R.id.red);//点击按钮变换画笔颜色
green=(Button) findViewById(R.id.green);
blue=(Button) findViewById(R.id.blue);
red.setOnClickListener(this);//添加监听
green.setOnClickListener(this);
blue.setOnClickListener(this);
//----------------------------------------------------------------------------------------- 3
//第二步:初始化声明的变量并设置一些属性
imageview=(ImageView) findViewById(R.id.imageview);// 获取布局文件上的图片控件
baseBitmap=Bitmap.createBitmap(700,640,Bitmap.Config.ARGB_8888);//Bitmap,位图,创建一个可以被修改的bitmap,Bitmap.Config.ARGB_8888是指定显示的色彩效果
canvas=new Canvas(baseBitmap);//创建画布,在位图上????、
//canvas.drawColor(Color.WHITE);//设置背景颜色
paint=new Paint();//获取画笔
paint.setStrokeWidth(3);//设置画笔的像素点
paint.setColor(Color.BLACK);//设置画笔颜色
//第三步:控件上添加一个触摸监听,用来监听用户的触摸轨迹
imageview.setOnTouchListener(this);
//-------------------------------------------------------------------------------------------4
}
public void onClick(View v){
switch(v.getId()){
case R.id.button1://当点击button1的时候
int progress1=progress_bar1.getProgress();//获取缓冲条进度,赋值给progress1,,int用来存数字
progress1=progress1+10;//每点击一次progress1加10
progress_bar1.setProgress(progress1);//把progress1的值传给progress_bar1显现出来
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;
progress_bar3.setProgress(progress3);
break;
//-------------------------------------------------------------------------------------------1
case R.id.red:
paint.setColor(Color.RED);//当单击按钮red的时候,画笔的颜色变成红色
break;
case R.id.green:
paint.setColor(Color.GREEN);
break;
case R.id.blue:
paint.setColor(Color.BLUE);
break;
}
}
//------------------------------------------------------------------------------------------3
//seekbar拖动时实现这个方法
public void onProgressChanged(SeekBar seekbar,int arg1, boolean arg2){//SeekBar seekbar记录点击的按钮
switch(seekbar.getId()){
case R.id.seek1:
r= seekbar1.getProgress();//记录红绿蓝数值进度,赋值给r,r是先前定义的,用来存进度条数值
textview1.setText(r+"");//setText就是设置textview里面的显示值,就把r给传进去显示,
//settext后面的参数必须是string类型的 r+“”相当于把他转化成string类型
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;
}
// r = seekbar1.getProgress();// 获取颜色
// g = seekbar2.getProgress();
// b = seekbar3.getProgress();
// view.setBackgroundColor(Color.rgb(r, g, b));
}
public void onStartTrackingTouch (SeekBar seekbar){
}
//停止拖动
public void onStopTrackingTouch (SeekBar seekbar){
}
//----------------------------------------------------------------------------------------2
@Override
public boolean onTouch(View v, MotionEvent event) {//v是显示控件,MotionEvent event是触摸事件
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:System.out.println("鼠标按下。。。。。。。");
startx=event.getX();//获取开始的坐标点
starty=event.getY();//获取开始的坐标点
break;
case MotionEvent.ACTION_MOVE:System.out.println("鼠标移动。。。。。。。");
float stopx=event.getX();//获取结束坐标
float stopy=event.getY();//获取结束坐标
canvas.drawLine(startx, starty, stopx, stopy, paint);//在画布上画
startx=event.getX();//画完后要对笔的位置进行时时的更新
starty=event.getY();
break;
case MotionEvent.ACTION_UP:System.out.println("鼠标离开。。。。。。。");
break;
}
imageview.setImageBitmap(baseBitmap);//把baseBitmap赋值给imageview
//触摸事件的时时响应更改为true
return true;
}
//--------------------------------------------------------------------------------------4
@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;
}
}
(activity_main.xml)代码:
<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" >
<Button
android:id="@+id/red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="红"
android:layout_weight="1"
/>
<Button
android:id="@+id/green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绿"
android:layout_weight="1"
/>
<Button
android:id="@+id/blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="蓝"
android:layout_weight="1"
/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<View
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:background="@color/black"
>
</View>
<ImageView
android:id="@+id/imageview"
android:layout_width="fill_parent"
android:layout_height="400dp"
/>
</FrameLayout>
</LinearLayout>
</TableLayout>
res-》drawable下btn.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners
android:radius="200px"
android:color="@color/blue"/>
</shape>
相关文章推荐
- Android Gradle Plugin指南(四)----测试
- Android Gradle Plugin指南(三)----依赖关系,Android库和多项目设置
- Android项目无法运行的解决方法
- Android 最火的快速开发框架androidannotations配置详解
- Android Gradle Plugin指南(二)——基本项目
- Android 导入第三方jar包
- Android 显示原理简介
- Android Gradle Plugin指南(一)——简介
- LayoutInflater使用
- Android studio下使用ShareSDK实现一键分享
- Android NDK生成共享库和静态库
- Android 坐标与宽高研究getLeft() getTop() getRight()和getBottom()
- android 小型音乐播放器(实现播放、下一首、上一首、自动播放、随机播放按钮、当前播放歌曲界面)
- Android ViewPager与Fragment的延时切换,取消预加载优化
- AndroidEventBus总结
- Android中两种序列化方式的比较Serializable和Parcelable
- Android中免Root实现Hook的Dexposed框架实现原理解析以及如何实现应用的热修复
- Android学习笔记-广播机制BoradcastReceiver
- Android SDK Android NDK Android Studio 官方下载地址
- Android实现ViewPager+Fragment