Android入门学习(2)
今天是Android学习的第二天,主要介绍了画板中一些简单功能的实现
基本功能:选择颜色、简单画图。
基本布局界面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.example.picture.paintview
android:id="@+id/paintview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="45dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="颜色选择" />
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>颜色列表下拉框的java代码如下:(代码源自网络,存在bug,尚在调试中,后面的监听还没有加进去)
public class MainActivity extends Activity {
private Spinner spinner;
private List<String> data_list;
private ArrayAdapter<String> arr_adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner);
spinner = (Spinner) findViewById(R.id.spinner);
//数据
data_list = new ArrayList<String>();
data_list.add("Red");
data_list.add("Blue");
data_list.add("Yellow");
data_list.add("Black");
//适配器
arr_adapter= new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, data_list);
//设置样式
arr_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//加载适配器
spinner.setAdapter(arr_adapter);
}
} 画布绘制图形java代码public class paintview extends View{
public paintview(Context context, AttributeSet attrs) {
super(context, attrs);
super.setBackgroundColor(Color.WHITE); //背景色
super.setOnTouchListener(new Touch()); //匿名类。Touch事件的监听
// TODO Auto-generated constructor stub
}
private List<Point> pointall=new ArrayList<Point>();
private class Touch implements OnTouchListener{
@Override
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;
}
}
protected void onDraw(Canvas canvas){
Paint p=new Paint(); //定义画笔
p.setColor(Color.RED); //定义颜色
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);
}
}
}
}
}MainActivity中的下拉列表代码还没有完成,目前只能实现绘制功能,没有颜色和笔刷尺寸的选择。
课堂笔记草稿:
adroid graphics包下
颜色选择框
radio button单选框
spinner下拉列表框学姐的android初步2中有讲
下拉列表的内容:xml文件;同时还应该在MainActivity.java加入下拉列表的监听:
当用户选中下拉列表中的某个选项之后可以使用Spinner类中提供的setOnItemClickListener()方法进行监听
字体填充:新建xml(values里面放配置、文字、样式表layout中放样式布局)
android entries
监听方法百度-->改变画笔颜色
画笔是一个触摸事件(onTouch)
color
paint画笔
canvas画布对象
paintview extends view(继承) 外部类
implemrnts接口
MotionEvent动作事件
将点放入一个队列,将移动的点添加进队列
画图:添加画笔、定义颜色、
p.setColor(Color.RED)获取到的是一个类,怎样反应为红色字符串数组
eclipse 画笔paint设置颜色
得到所有的点,开始绘图(两个点)
iterator迭代器:保存的点赋给迭代器(记录初始点和末点连接),last!==null时将下一个点赋给first,first=last //这个功能不仅限于android
imageview图片的监听事件:获取到图片的tag(tag是否可以写为中文)Color.RED,
配置组件:
所有控件都继承view,
加载方式 包名+java类名
画笔粗细的配置对话框 android入门11
首先设置想要放置的内容;setItems(reason中放置退款原因)
监听器是一个点击事件
scrollview主布局 可以滚动的控件都可以放进去
button添加到布局中(首先通过id找到页面布局;设置n个布局,layout.addview for循环进行添加)
listview 适配器
音乐内容(imagaedio):线程 R.文件夹.文件名
图形绘制