实现一个简单的android画板功能
主要功能有:画图,更换画布颜色,更换画笔颜色,笔迹粗细,当然还有最重要的放歌功能。
画图界面:
界面代码
<RelativeLayout 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="com.example.nono.second" > <ImageButton android:id="@+id/music" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:background="#00000000" android:layout_marginTop="0dp" android:src="@drawable/start" /> <com.example.nono.paintview android:id="@+id/paintview" android:layout_width="200dp" android:layout_height="200dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignRight="@+id/music" android:layout_below="@+id/music" /> //画板 <Spinner android:id="@+id/colorselect" android:layout_width="33dp" android:layout_height="33dp" android:layout_alignLeft="@+id/paintview" android:layout_alignTop="@+id/music" android:layout_marginLeft="23dp" android:entries="@array/colors" />//画笔颜色下拉选择框 <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/colorselect" android:layout_alignLeft="@+id/paintview" android:text="画布" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/paintview" android:layout_toLeftOf="@+id/bkcolorselect" android:text="画笔颜色" /> <Spinner android:id="@+id/bkcolorselect" android:layout_width="33dp" android:layout_height="33dp" android:layout_above="@+id/paintview" android:layout_centerHorizontal="true" android:entries="@array/colors" />//画布颜色选择框 <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/paintview" android:layout_toRightOf="@+id/bkcolorselect" android:text="粗细" /> <Spinner android:id="@+id/wid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/paintview" android:layout_alignTop="@+id/music" android:layout_toLeftOf="@+id/music" android:layout_toRightOf="@+id/textView4" android:entries="@array/num" />//画笔粗细选择框 </RelativeLayout>
下拉框显示的值配置在arrays.xml中
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="colors"> <item>red</item> <item>yellow</item> <item>black</item> <item>blue</item> <item>white</item> </string-array> <string-array name="num"> <item>5</item> <item>10</item> <item>15</item> <item>20</item> <item>25</item> </string-array> </resources>
功能方面
首先是画图功能,主要使用三个类(Color,Paint,Canvas)
画图功能其实是将画笔走过的点依次连接起来形成线条。
public boolean onTouch(View v, MotionEvent e) {
// TODO Auto-generated method stub
Point p=new Point((int)e.getX(),(int)e.getY());
if(e.getAction()==e.ACTION_DOWN){ //当按下
pointall=new ArrayList<Point>();
pointall.add(p);
}
else if(e.getAction()==e.ACTION_UP){//当抬起
pointall.add(p);
paintview.this.postInvalidate(); //重绘
}
else if(e.getAction()==e.ACTION_MOVE){
pointall.add(p); //移动时候
paintview.this.postInvalidate(); //重绘
}
return true;
}
该方法主要是将画笔走过的点依次记录在链表里面
再利用下面的方法依次将链表中的点连接起来形成线条
if(pointall.size()>1){
Iterator<Point> iter=pointall.iterator();// 现在有坐标点保存的时候可以开始进行绘图
Point first=null;
Point last=null;
while(iter.hasNext()){
if(first==null){
first=(Point)iter.next();
}
else{
if(last!=null){
first=last; //将下一个坐标点赋给上面的
}
last=(Point)iter.next(); //不停下指
canvas.drawLine(first.x, first.y, last.x, last.y,p);
}
}
}
}
更换画笔颜色和粗细
这个功能的实现是利用setColor()和setStrokeWidth()方法改变Paint类的color和width的值。
根据用户所选择的值传递相关参数,对颜色和粗细进行改变:
static void setwidth(int i){
p.setStrokeWidth(i);
}
static void setcolor(int i){
switch(i){
case 1:p.setColor(Color.RED);break;
case 2:p.setColor(Color.YELLOW);break;
case 3:p.setColor(Color.BLACK);break;
case 4:p.setColor(Color.BLUE);break;
case 5:p.setColor(Color.WHITE);break;
}
}
更换画布颜色
这个功能的实现刚开始我以为跟画笔颜色更换差不多,后来发现只需要改变界面上组件的背景属性便可以解决
public class second extends Activity{
private com.example.nono.paintview pa;//建立画板的对象
.............
@Override
protected void onCreate(Bundle savedInstanceState) {
pa =(com.example.nono.paintview) findViewById(R.id.paintview);//与界面上画布绑定
...........
}
private void setlistener(){
...........
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
String[] colors = getResources().getStringArray(R.array.colors);
Toast.makeText(second.this, "你点击的是:"+colors[pos], 2000).show();
if(colors[pos].equals("red"))
{spinner.setBackgroundColor(Color.RED);//改变下拉框所选择的颜色
pa.setBackgroundColor(Color.RED);}//改变画布颜色
else if(colors[pos].equals("yellow"))
{spinner.setBackgroundColor(Color.YELLOW);
pa.setBackgroundColor(Color.YELLOW);}
else if(colors[pos].equals("black"))
{spinner.setBackgroundColor(Color.BLACK);
pa.setBackgroundColor(Color.BLACK);
}
else if(colors[pos].equals("blue"))
{spinner.setBackgroundColor(Color.BLUE);
pa.setBackgroundColor(Color.BLUE);
}
else if(colors[pos].equals("white"))
{spinner.setBackgroundColor(Color.WHITE);
pa.setBackgroundColor(Color.WHITE);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
});
}
}
音乐播放功能
对图片按钮的监听,根据点击改变图片,并调用播放或停止音乐的方法
music.setOnClickListener(new OnClickListener() {
public void onClick(View v){
count++;
if(count%2==1)
{ music.setImageDrawable(getResources().getDrawable(R.drawable.stop)); //改变按钮图标
start(v);}
else
{music.setImageDrawable(getResources().getDrawable(R.drawable.start)); //改变按钮图标
stop(v);
}
}
});//对播放音乐按钮的监听
public void start(View v) {
//找到指定MP3资源
this.myMediaPlayer=MediaPlayer.create(getApplicationContext(), R.raw.qt);
//播放完毕处理
this.myMediaPlayer.setOnCompletionListener(new OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer media) {
second.this.playflag=false;//播放完毕
media.release();//释放所有状态
}
});
if(this.myMediaPlayer!=null){
this.myMediaPlayer.stop();//停止播放
}
try {
if(!this.myMediaPlayer.isPlaying()){
this.myMediaPlayer.prepare();
this.myMediaPlayer.start();
// Mp3Activity.this.text.setText("正在播放音频文件");
}
}
catch (IOException e) {
// TODO Auto-generated catch block
// this.text.setText("文件播放出现异常"+e);
}
}
public void stop(View v) {
if(second.this.myMediaPlayer!=null){
second.this.myMediaPlayer.stop();
// Mp3Activity.this.text.setText("停止播放音频文件....");
}
}
}
改变画布颜色,画笔颜色、粗细,听着歌的画图界面: