您的位置:首页 > 移动开发 > Android开发

利用Bimap,canvas处理图片并保存(源码)

2011-11-14 09:38 465 查看
android怎么样对一张图片处理,并保存好。
我从最简单的使用开始,在一张图片上画直线,然后显示这张画好的图。

首先要知道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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息