最近一直很忙,学Android ,快一个月了到现在还没能入门..真挺尴尬的 ,不说废话了.
今天整理下画图
1.Bitmap,可以来自资源/文件,也可以在程序中(动态创建),实际上的功能相当于图片的存储空间;
2.Canvas,紧密与Bitmap联系,把Bitmap比喻内容的话,那么Canvas就是提供了众多方法操作Bitamp的平台,你可以理解为一块画布
.
3.Paint,与Canvas紧密联系,是"画板"上的笔刷工具,也用于设置View控件上的样式;
4.Drawable,如果说前三者是看不见地在内存中画图,那么Drawable就是把前三者绘图结果表现出来的接口。
1. 绘画bitmap资源
2. 动态创建画布,绘画文字,以及设置文字的样式
3. 在控件上画图
UI的XML代码
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ToggleButton
android:id="@+id/btn_show_res"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textOff="@string/showres"
android:textOn="@string/dieres"
/>
<ToggleButton
android:id="@+id/btn_draw_res"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/drawres"
android:textOff="@string/show_res"
android:textOn="@string/die_res"
/>
<Button
android:id="@+id/btn_draw_widget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/drawwidget"
/>
<ImageView android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/description"/>
</LinearLayout>
介绍一下新出现的控件
ToggleButton
google的overview是这样来描述的
Displays checked/unchecked states as a button with a "light" indicator and by default accompanied with the text "ON" or "OFF".
用一个"灯"来检查指示这个按钮是处于ON还是OFF状态.在这里我们用到了它的两个属性
android:textOff : 当按钮处于Off状态时,按钮的值
android:textOn:当按钮处于ON状态时,按钮的值
Java代码
public class mDrawable extends Activity {
ImageView imageView;
Button drawOnWidget_btn;
ToggleButton show_btn,draw_btn;
Resources res;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mdrawable);
PreInitUI();
SetBtnClick();
}
private void PreInitUI(){
imageView = (ImageView)this.findViewById(R.id.ImageView01);
show_btn = (ToggleButton)this.findViewById(R.id.btn_show_res);
draw_btn = (ToggleButton)this.findViewById(R.id.btn_draw_res);
drawOnWidget_btn = (Button)this.findViewById(R.id.btn_draw_widget);
res = this.getResources();
}
private void SetBtnClick(){
show_btn.setOnClickListener(listener);
draw_btn.setOnClickListener(listener);
drawOnWidget_btn.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener() {
private int count = 0;
private int count1 = 0;
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_show_res:{
// 跟findViewById 差不多作用,寻找某个资源,这里是找到Bitmap资源
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.ic_launcher);
if(count % 2 == 0){
// 设置某个 View 是否可见
imageView.setVisibility(View.VISIBLE);
imageView.setImageBitmap(bmp);
}else{
imageView.setVisibility(View.GONE);
}
count ++;
break;
}case R.id.btn_draw_res:{
//Bitmap 读取资源文件
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.ai);
Bitmap newbmp = Bitmap.createBitmap( 450, 200, Config.ARGB_8888 );
//Canvas 画布类
Canvas canvasTemp = new Canvas( newbmp );
canvasTemp.drawColor(Color.BLACK);
//Paint 画笔类
Paint paint = new Paint();
String familyName = "宋体";
Typeface font = Typeface.create(familyName,Typeface.BOLD);
paint.setColor(Color.BLUE);
paint.setTypeface(font);
paint.setTextSize(22);
// 设置文字,以及文字的显示位置
canvasTemp.drawText("对你爱爱爱不完,相爱原本就是这么难!", 0, 100, paint);
canvasTemp.drawBitmap(bmp, 50, 50, paint);
if(count%2==1){
imageView.setVisibility(View.GONE);
}else{
imageView.setImageBitmap(newbmp);
imageView.setVisibility(View.VISIBLE);
}
count1++;
break;
}case R.id.btn_draw_widget:{
// 创建一个与drawOnWidget_btn长宽想同的Bitmap
Bitmap newbmp = Bitmap.createBitmap( drawOnWidget_btn.getWidth(), drawOnWidget_btn.getHeight(), Config.ARGB_8888 );
Canvas canvasTemp = new Canvas(newbmp);
canvasTemp.drawColor(Color.GRAY);
Paint p = new Paint();
String familyName = "宋体";
Typeface font = Typeface.create(familyName, Typeface.BOLD);
p.setColor(Color.RED);
p.setTypeface(font);
p.setTextSize(20);
canvasTemp.drawText("在控件上画图", 100, 30, p);
Drawable drawable = new BitmapDrawable(newbmp);
Toast.makeText(getApplication(), "btn_draw_widget start!", Toast.LENGTH_SHORT).show();
drawOnWidget_btn.setBackgroundDrawable(drawable);
break;
}
}
}
};
}
3092

被折叠的 条评论
为什么被折叠?



