canvas教程

Canvas.drawBitmap()画图偏移

字号+ 作者:H5之家 来源:H5之家 2015-10-28 15:18 我要评论( )

Canvas.drawBitmap()画图偏移 相关文章 相关文章 android上文本文档阅读器的分页问题 android下文字输出格式,类似LOGFONT 我需要实现一个揭开幕布显示一张图片的功能,一开始我用ImageView显示一张灰色的幕布(该幕布用指定要显示的图片构造大小),然后通

Canvas.drawBitmap()画图偏移

相关文章

相关文章

android上文本文档阅读器的分页问题

android下文字输出格式,类似LOGFONT

我需要实现一个揭开幕布显示一张图片的功能,一开始我用ImageView显示一张灰色的幕布(该幕布用指定要显示的图片构造大小),然后通过触屏事件(假设是DOWN)从指定要显示的图片切一块(默认指定是10×10像素),最后把切出来的作为源图,通过Canvas.drawBitmap(src,left,top,paint)画到幕布上,结果是画出来的图片位置与我在屏幕上点击的位置有偏移,其中left和top用的就是getX()和getY()的值,这是什么原因呢?
------Solutions------
package com.zhutieju.openphoto;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;

public class OpenPhotoActivity extends Activity {
Button previousBtn,nextBtn;
ImageView picture;
Cursor cursor;
String imageFilePath;
int fileColumn;
Bitmap pictureBmp,cloth;
Canvas canvas;
Paint paint;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open_photo);
        //获取各个控件对象
        previousBtn = (Button)findViewById(R.id.previous);
        nextBtn = (Button)findViewById(R.id.next);
        picture = (ImageView)findViewById(R.id.picture);
        //获取cursor
        String[] columns = {Media.DATA,Media._ID,Media.TITLE,Media.DISPLAY_NAME};
        cursor = managedQuery(Media.EXTERNAL_CONTENT_URI, columns, null, null, null);     
        //通过cursor获取各个列的索引
        fileColumn = cursor.getColumnIndexOrThrow(Media.DATA);
        //ImageView触屏事件
        picture.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
float downX = 0;
float downY = 0;
float upX = 0;
float upY = 0;
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = event.getX();
downY = event.getY();
pictureBmp = Bitmap.createBitmap(getBitmap(imageFilePath), (int)downX, (int)downY, 20, 20);

canvas.drawBitmap(pictureBmp, event.getX(),event.getY() , paint);
picture.setImageBitmap(cloth);
break;
case MotionEvent.ACTION_MOVE:

break;
case MotionEvent.ACTION_UP:
upX = event.getX();
upY = event.getY();

break;
default:
break;
}
return true;
}
});
        
        //为previousBtn添加点击事件
        previousBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(cursor.moveToNext()) {
imageFilePath = cursor.getString(fileColumn);
            //获取幕布
pictureBmp = getClothBmp();
//显示幕布或者图片
picture.setImageBitmap(pictureBmp);
} else {
cursor.moveToFirst();
            //获取幕布
pictureBmp = getClothBmp();
        //显示幕布
picture.setImageBitmap(pictureBmp);
}
}
});
        //为nextBtn添加点击事件
        nextBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/*if(cursor.moveToPrevious()) {
imageFilePath = cursor.getString(fileColumn);
pictureBmp = getBitmap(imageFilePath);
picture.setImageBitmap(pictureBmp);
            //获取幕布
            clothBmp = getClothBmp();
        //显示幕布
        cloth.setImageBitmap(clothBmp);
} else {
cursor.moveToLast();
imageFilePath = cursor.getString(fileColumn);
pictureBmp = getBitmap(imageFilePath);
picture.setImageBitmap(pictureBmp);
            //获取幕布
            clothBmp = getClothBmp();
        //显示幕布
        cloth.setImageBitmap(clothBmp);
}*/
//drawTransparent(clothBmp);
}
});
    }
    @Override
    protected void onResume() {
    paint = new Paint();
    // TODO Auto-generated method stub
    super.onResume();
        //查询并显示图像
        if(cursor.moveToFirst()) {
        imageFilePath = cursor.getString(fileColumn);
        pictureBmp = getBitmap(imageFilePath);
            //获取幕布
        pictureBmp = getClothBmp();
        //显示幕布
            picture.setImageBitmap(pictureBmp);
        }  
    }
    /**
     * 通过路径返回以屏幕宽高比例的位图
     * @param imageFilePath
     * @return
     */
    private Bitmap getBitmap(String imageFilePath) {
    Bitmap bmp;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    bmp = BitmapFactory.decodeFile(imageFilePath, options);
    int width = options.outWidth / getWindowManager().getDefaultDisplay().getWidth();
    int height = options.outHeight / getWindowManager().getDefaultDisplay().getHeight();
    options.inSampleSize = width>height?width:height;
    options.inJustDecodeBounds = false;
    bmp = BitmapFactory.decodeFile(imageFilePath, options);
    return bmp;
    }
    /**
     * 获取一个所需要的幕布
     * @return
     */
    private Bitmap getClothBmp() {
    cloth = Bitmap.createBitmap(pictureBmp.getWidth(),pictureBmp.getHeight(),pictureBmp.getConfig());
    canvas = new Canvas(cloth);
    canvas.drawColor(Color.GRAY);
    //canvas.drawBitmap(clothBmp1, 10, 10,null);
    return cloth;
    }

}

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • HTML5 canvas 作画板画图 可以做电子白板

    HTML5 canvas 作画板画图 可以做电子白板

    2017-04-27 12:02

  • Android画图学习免费下载

    Android画图学习免费下载

    2017-04-27 11:01

  • CAD迷你画图 V2017R4 官方版下载

    CAD迷你画图 V2017R4 官方版下载

    2017-04-27 10:03

  • html5canvas画图例子

    html5canvas画图例子

    2017-04-24 15:02

网友点评