Android入门之画图详解
作者:shichen2014 时间:2023-11-09 11:47:47
前文常用的控件介绍了不少,现在就来讨论一下手机开发中常用到的画图。要掌握Android的画图,首先就要了解一下,基本用到的如下一些图形接口:
1.Bitmap,可以来自资源/文件,也可以在程序中创建,实际上的功能相当于图片的存储空间;
2.Canvas,紧密与Bitmap联系,把Bitmap比喻内容的话,那么Canvas就是提供了众多方法操作Bitamp的平台;
3.Paint,与Canvas紧密联系,是"画板"上的笔刷工具,也用于设置View控件上的样式;
4.Drawable,如果说前三者是看不见地在内存中画图,那么Drawable就是把前三者绘图结果表现出来的接口。Drawable多个子类,例如:位图(BitmapDrawable)、图形(ShapeDrawable)、图层(LayerDrawable)等。
本文主要讲解如何在ImageView画图,以及如何直接在Button(继承View的控件)上面绘制自定义图像。如下图所示:
直接把资源图片画出来:
在ImageView上画图以及绘字:
直接在控件背景上画图:
main.xml的源码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/Button01" android:layout_width="fill_parent" android:layout_height="44px" android:text="显示资源图片"></Button>
<Button android:id="@+id/Button02" android:layout_width="fill_parent" android:layout_height="44px" android:text="显示并绘画资源图片"></Button>
<Button android:id="@+id/Button03" android:layout_height="44px" android:layout_width="fill_parent" android:text="在控件上绘图"></Button>
<ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</LinearLayout>
Java程序的源码如下:
package com.testDraw;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class testDraw extends Activity {
ImageView iv;
Button btn1,btn2,btn3,btn4;
Resources r;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv=(ImageView)this.findViewById(R.id.ImageView01);
btn1=(Button)this.findViewById(R.id.Button01);
btn2=(Button)this.findViewById(R.id.Button02);
btn3=(Button)this.findViewById(R.id.Button03);
btn1.setOnClickListener(new ClickEvent());
btn2.setOnClickListener(new ClickEvent());
btn3.setOnClickListener(new ClickEvent());
r = this.getResources();
}
class ClickEvent implements View.OnClickListener {
public void onClick(View v) {
if(v==btn1)//显示资源图片
{//功能等效
//iv.setBackgroundResource(R.drawable.icon);//打开资源图片
Bitmap bmp=BitmapFactory.decodeResource(r, R.drawable.icon);//打开资源图片
iv.setImageBitmap(bmp);
}
else if(v==btn2)//显示并绘画资源图片
{
Bitmap bmp=BitmapFactory.decodeResource(r, R.drawable.icon);//只读,不能直接在bmp上画
Bitmap newb = Bitmap.createBitmap( 300, 300, Config.ARGB_8888 );
Canvas canvasTemp = new Canvas( newb );
canvasTemp.drawColor(Color.TRANSPARENT);
Paint p = new Paint();
String familyName ="宋体";
Typeface font = Typeface.create(familyName,Typeface.BOLD);
p.setColor(Color.RED);
p.setTypeface(font);
p.setTextSize(22);
canvasTemp.drawText("写字。。。",50,50,p);
canvasTemp.drawBitmap(bmp, 50, 50, p);//画图
iv.setImageBitmap(newb);
}
else if(v==btn3)//直接在Button上绘图
{
Bitmap newb = Bitmap.createBitmap( btn3.getWidth(), btn3.getHeight(), Config.ARGB_8888 );
Canvas canvasTemp = new Canvas( newb );
canvasTemp.drawColor(Color.WHITE);
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("写字。。。", 30, 30, p);
Drawable drawable = new BitmapDrawable(newb);
btn3.setBackgroundDrawable(drawable);
}
}
}
}
标签:Android,画图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#实现SMTP邮件附件发送功能详解
2022-08-14 10:09:38
![](https://img.aspxhome.com/file/2023/9/89829_0s.png)
C#中decimal保留2位有效小数的实现方法
2023-01-30 07:35:13
JAVA SPI特性及简单应用代码实例
2021-11-11 14:54:54
![](https://img.aspxhome.com/file/2023/4/63674_0s.png)
常用Eclipse快捷方式(推荐)
2022-01-22 04:37:56
Android 完整购物商城界面的实现案例
2023-06-14 01:33:06
![](https://img.aspxhome.com/file/2023/1/84721_0s.png)
Android table布局开发实现简单计算器
2021-07-15 13:05:28
![](https://img.aspxhome.com/file/2023/6/137476_0s.jpg)
Java中JMM与volatile关键字的学习
2022-03-24 00:12:00
![](https://img.aspxhome.com/file/2023/2/62202_0s.png)
java利用jacob将word转pdf
2023-02-05 08:23:03
![](https://img.aspxhome.com/file/2023/0/75510_0s.jpg)
经典排序算法之冒泡排序(Bubble sort)代码
2021-06-08 06:06:18
![](https://img.aspxhome.com/file/2023/8/96418_0s.gif)
在IDEA中 实现给main方法附带参数的操作
2022-10-23 14:42:03
![](https://img.aspxhome.com/file/2023/7/71237_0s.jpg)
java String类常量池分析及"equals"和"==”区别详细介绍
2021-11-28 01:54:48
深入理解C#中常见的委托
2022-01-15 07:53:05
![](https://img.aspxhome.com/file/2023/6/104216_0s.jpg)
C# 中对象序列化XML的方法
2021-11-12 13:51:55
![](https://img.aspxhome.com/file/2023/5/82215_0s.png)
java多线程Thread-per-Message模式详解
2022-12-23 16:55:00
![](https://img.aspxhome.com/file/2023/1/92691_0s.png)
Android ChipGroup收起折叠效果实现详解
2023-07-29 01:58:20
![](https://img.aspxhome.com/file/2023/3/128993_0s.jpg)
C# 图片与Base64码的相互转化问题(代码详解)
2021-11-30 22:56:16
SpringBoot工程下使用OpenFeign的坑及解决
2022-08-24 18:00:38
C#自定义针对URL地址的处理类实例
2022-09-12 16:54:02
关于@Autowired注解和静态方法及new的关系
2021-07-16 13:08:06
Struts2中异常处理机制分析
2023-11-17 21:54:32