利用Bimap,canvas处理图片并保存(源码)
来源:互联网 发布:域名注册好了怎么备案 编辑:程序博客网 时间:2024/04/28 14:18
我从最简单的使用开始,在一张图片上画直线,然后显示这张画好的图。
首先要知道Bimap,canvas的用法, 它的详细用法大家可以网上去找资料,重要的是 我要说下canvas有个构造函数Canvas (Bitmap bitmap);我们对这个 canvas 的操作实际上就是操作它的Bitmap;
主Activity 的主要代码如下:
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
public class DrawActivity extends Activity {
static boolean drawFlag=false;
private Button show_btn; //清除所画图像按键
private Button line_btn;//开始画直线按钮
private ImageView image;
private DrawView drawView;
private Bitmap bimap;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
show_btn=(Button) findViewById(R.id.clear_btn);
line_btn=(Button) findViewById(R.id.length_btn);
line_btn.setOnClickListener(new ClickEvent());
show_btn.setOnClickListener(new ClickEvent());
image=(ImageView) findViewById(R.id.imageView1);
drawView=(DrawView)findViewById(R.id.drawView);
//如果不用copy的方法,直接引用会对资源文件进行修改,而android是不允许在代码里修改res文件里的图片
bimap=BitmapFactory.decodeResource(getResources(), R.drawable.background).copy(Bitmap.Config.ARGB_8888, true);
}
class ClickEvent implements View.OnClickListener {
public void onClick(View v) {
if(v==show_btn){
image.setImageBitmap(bimap);//显示图片
}
if(v==line_btn){
drawView.setBitmap(bimap);//传我们需要处理的bimap给画图类
DrawActivity.drawFlag=true; //开始画图的标志
}
}
}
}
然后在在画图类的Ondraw()函数中处理图片,在图片上画直线"
package com.android.draw;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
public class DrawView extends ImageView {
private float beginX=0,beginY=0,endX=0,endY=0;//画直线的起点和终点的X和Y坐标
private int eventflag = 0;//触屏事件点击
private Canvas canvasself;//用于保存所画图像的画布
private Bitmap bimap;//用于保存所画图像的图画
public DrawView(Context context) {
super(context);
}
/*******
在XML中使用自定义View必须要使用含AttributeSet变量参数的构造函数
******/
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setBitmap(Bitmap b){
bimap=b;
canvasself = new Canvas(bimap);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(DrawActivity.drawFlag){
Paint p=new Paint();
/*********设置画笔********/
p.setColor(Color.WHITE);
p.setStyle(Paint.Style.STROKE);
p.setAntiAlias(true);
canvas.drawBitmap(bimap, 0, 0,null);//把之前画出来保存后的图再画出来
canvas.drawLine(beginX, beginY, endX, endY, p);//画中间长得线段
if(eventflag==3){
//这里将直线画到canvasself上,它就保存在了bimap这张图上
canvasself.drawLine(beginX, beginY, endX, endY, p);
}
}
}
画图类的触屏事件处理
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
endX = event.getX();
endY = event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
eventflag = 1;
beginX = endX;
beginY = endY;
invalidate();
break;
case MotionEvent.ACTION_MOVE:
eventflag = 2;
invalidate();
break;
case MotionEvent.ACTION_UP:
eventflag = 3;
invalidate();
break;
}
return true;
}
main.xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/length_btn"
android:layout_height="wrap_content"
android:text="画直线"
android:layout_width="wrap_content"/>
<TextView android:text="画图区"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.android.draw.DrawView
android:id="@+id/drawView"
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="fill_parent" >
</com.android.draw.DrawView>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/clear_btn"
android:text="显示"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView android:text="显示画图区"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView android:id="@+id/imageView1"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="wrap_content" />
</LinearLayout>
</LinearLayout>
源码在我的这篇博客里有
http://www.eoeandroid.com/thread-114497-1-1.html
- 利用Bimap,canvas处理图片并保存(源码)
- Android: 利用Bimap,canvas处理图片(画直线)
- 利用HTML5中Canvas处理并存储图片
- 利用HTML5中Canvas处理并存储图片
- h5 利用canvas手写签名并保存
- 利用HTML5d的Canvas绘制签名区域,保存为图片
- 用html5的canvas生成图片并保存到本地
- HTML5 canvas画图并保存成图片插件
- [Guava源码日报](11)BiMap
- android图片压缩处理,并保存
- 基于Qt与OpenCV的图片读取、保存,并利用Canny进行简单的边缘处理结果显示
- BiMap源码解析
- iPhone开发笔记(12)利用.net实现服务端接收上传的图片并保存
- 利用微信jssdk上传图片,并保存到本地
- Android Canvas 保存为图片
- andrid canvas 保存成图片
- 基于canvas的图片保存
- 怎样将html5中利用canvas绘制的图像在服务端保存为图片?
- c#DIY随机数类
- 使用PDFbox和jepdal工具类,把PDF文件转成图片
- 让你提升命令行效率的 Bash 快捷键 [完整版]
- silk 中的lpc系数有插值没
- 注重接口,而不是实现 (原则之一)
- 利用Bimap,canvas处理图片并保存(源码)
- TLV格式及编码
- ASP.NET长文章分页显示函数
- [转]用PHP会话(Session)实现用户登陆功能
- ASCII码对照表[备忘]
- sap 一次性供应商 供应商账户组
- dedecms 常用标签
- aspnet常用方法
- [转]深入理解软件包的配置、编译与安装