自定义View实现图片的绘制、旋转、缩放
1、图片
把一张JPG图片改名为image.jpg,然后拷贝到项目的res-drawable中。
2、activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"> <Button android:id="@+id/buttonLeft"
android:text="图片向左移动"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button android:id="@+id/buttonRight"
android:text="图片向右移动"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button android:id="@+id/buttonRotationLeft"
android:text="图片向左旋转"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button android:id="@+id/buttonRotationRight"
android:text="图片向右旋转"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button android:id="@+id/buttonNarrow"
android:text="图片缩小"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button android:id="@+id/buttonEnlarge"
android:text="图片放大"
android:layout_width="fill_parent"
android:layout_height="wrap_content" /> </LinearLayout>
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Canvas;
import android.widget.LinearLayout;
import android.widget.Button; public class MainActivity extends Activity {
ImageView imageView = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 动态加载图片到LinearLayout中
imageView = new ImageView(this);
LinearLayout ll = (LinearLayout) findViewById(R.id.imageid);
ll.addView(imageView);
// 向左移动
Button btnLeft = (Button) findViewById(R.id.buttonLeft);
btnLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
imageView.setPosLeft();
}
});
// 向右移动
Button btnRight = (Button) findViewById(R.id.buttonRight);
btnRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
imageView.setPosRight();
}
}); // 向左旋转
Button btnRotationLeft = (Button)findViewById(R.id.buttonRotationLeft);
btnRotationLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
imageView.setRotationLeft();
}
});
// 向右旋转
Button btnRotationRight = (Button)findViewById(R.id.buttonRotationRight);
btnRotationRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
imageView.setRotationRight();
}
}); // 放大图片
Button btnEnlarge = (Button)findViewById(R.id.buttonEnlarge);
btnEnlarge.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
imageView.setEnlarge();
}
});
// 缩小图片
Button btnNarrow = (Button)findViewById(R.id.buttonNarrow);
btnNarrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
imageView.setNarrow();
}
});
} // 自定义图片View
class ImageView extends View {
private Paint paint = null; // 画笔
private Bitmap bitmap = null; // 图片位图
private Bitmap bitmapDisplay = null;
private Matrix matrix = null;
private int nBitmapWidth = 0; // 图片的宽度
private int nBitmapHeight = 0; // 图片的高度
private int nPosX = 120; // 图片所在的位置X
private int nPosY = 10; // 图片所在的位置Y
private float fAngle = 0.0f; // 图片旋转
private float fScale = 1.0f; // 图片缩放 1.0表示为原图 public ImageView(Context context) {
super(context); paint = new Paint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG); // 加载需要操作的图片
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
bitmapDisplay = bitmap; matrix = new Matrix();
// 获取图片高度和宽度
nBitmapWidth = bitmap.getWidth();
nBitmapHeight = bitmap.getHeight();
} // 向左移动
public void setPosLeft() {
nPosX -= 10;
}
// 向右移动
public void setPosRight() {
nPosX += 10;
} // 向左旋转
public void setRotationLeft() {
fAngle--;
setAngle();
}
// 向右旋转
public void setRotationRight() {
fAngle++;
setAngle();
} // 图片放大
public void setEnlarge() {
if (fScale < 2) {
fScale += 0.1f;
setScale();
}
}
// 图片缩小
public void setNarrow() {
if (fScale > 0.5) {
fScale -= 0.1f;
setScale();
}
} // 设置旋转比例
private void setAngle() {
matrix.reset();
matrix.setRotate(fAngle);
bitmapDisplay = Bitmap.createBitmap(bitmap,0,0,nBitmapWidth,nBitmapHeight,matrix,true);
} // 设置缩放比例
private void setScale() {
matrix.reset();
matrix.postScale(fScale, fScale);
bitmapDisplay = Bitmap.createBitmap(bitmap,0,0,nBitmapWidth,nBitmapHeight,matrix,true);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bitmapDisplay, nPosX, nPosY, paint);
invalidate();
}
}
}

自定义View实现图片的绘制、旋转、缩放的更多相关文章
- 安卓自定义View实现图片上传进度显示(仿QQ)
首先看下我们想要实现的效果如下图(qq聊天中发送图片时的效果): 再看下图我们实现的效果: 实现原理很简单,首先我们上传图片时需要一个进度值progress,这个不管是自己写的上传的方法还是使用第三方 ...
- 自定义View实现图片热区效果
我司主要从事工业物联网领域软件的开发,现有个需求,在外废品处理时需要对产品的不良位置进行标记,点选图片实现图片网格的着色功能. 需求是通过自定义view来实现,实现思路如下: 首先将点击的小方格对象实 ...
- Android -- 自定义View小Demo,绘制四位数随机码(一)
1,现在有这样一个需求,实现显示随机随机数可能在代码中直接很简单的就实现了,但是现在我们直接自定义View来实现这个效果,那么我们来分析一波吧,我们允许开发者自己设置这个textview的大小,颜色, ...
- Android -- 自定义View小Demo,绘制钟表时间(一)
1,昨天刚看了hongyang大神推荐的自定义时钟效果(传动门:http://www.jianshu.com/users/a45d19d680af/),效果还是不错的,自己又在github上找了找,发 ...
- android 缩放平移自定义View 显示图片
1.背景 现在app中,图片预览功能肯定是少不了的,用户基本已经形成条件反射,看到小图,点击看大图,看到大图两个手指开始进行放大,放大后,开始移动到指定部位~~~ 我相信看图的整个步骤,大家或者说用户 ...
- 自定义view(14)使用Path绘制复杂图形
灵活使用 Path ,可以画出复杂图形,就像美术生在画板上画复杂图形一样.程序员也可以用代码实现. 1.样板图片 这个是个温度计,它是静态的,温度值是动态变化的,所以要自定义个view.动态显示值,温 ...
- Android自定义View——QQ音乐中圆形旋转碟子
1.在onMeasure中测量整个View的宽和高后,设置宽高 2.获取我们res的图片资源后,在ondraw方法中进行绘制圆形图片 3.通过Handler发送Runnable在主线程中更新UI,达到 ...
- Android 自定义 View 圆形进度条总结
Android 自定义圆形进度条总结 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 微信公众号:牙锅子 源码:CircleProgress 文中如有纰漏,欢迎大家留言指出. 最近 ...
- 安卓自定义View教程目录
基础篇 安卓自定义View基础 - 坐标系 安卓自定义View基础 - 角度弧度 安卓自定义View基础 - 颜色 进阶篇 安卓自定义View进阶 - 分类和流程 安卓自定义View进阶 - Canv ...
随机推荐
- BASM遵循的规则
任何情况下,在寄存器的使用上,BASM遵循如下的规则: ASM 语句执行过程中,必须保存EDI.ESI.ESP.EBP.EBX 的值(5个寄存器,意思是可以用,但最后得恢复成原模原样). ASM ...
- Sublime Text 2安装汉化破解、插件包安装教程
原文地址: Sublime Text 2安装汉化破解.插件包安装教程_百度经验 http://jingyan.baidu.com/article/ff4116259b057c12e48237b8.ht ...
- [PHP]利用XAMPP搭建本地服务器, 然后利用iOS客户端上传数据到本地服务器中(三. PHP端代码实现)
一.安装XAMPP http://www.cnblogs.com/lidongxu/p/5256330.html 二. 配置MySql http://www.cnblogs.com/lidongx ...
- http://blog.csdn.net/lvyuanj/article/details/51235135
http://blog.csdn.net/lvyuanj/article/details/51235135
- Spring Boot MyBatis 通用Mapper插件集成
Mybatis在使用过程中需要三个东西,每张表对应一个XXMapper.java接口文件,每张表对应一个XXMapper.xml文件,每张表对应一个Entity的Java文件. 其中XXMappe ...
- 【资料分享】 OpenCV精华收藏
OpenCV精华收藏 SkySeraph Dec.29th 2010 HQU Email:zgzhaobo@gmail.com QQ:452728574 Latest Modified Dat ...
- 图像分类之特征学习ECCV-2010 Tutorial: Feature Learning for Image Classification
ECCV-2010 Tutorial: Feature Learning for Image Classification Organizers Kai Yu (NEC Laboratories Am ...
- Ubuntu 14.10下安装深度音乐客户端
很多刚从windows系统投靠到ubuntu的机油,在听音乐时不是很舒心.毕竟ubuntu软件中心的很多影音软件都是国外的朋友编写的,所以很多时候国内的朋友用着很不舒服.今天给大家推荐的是国内开发者针 ...
- Maven下使用Jetty进行Debug
1.环境和条件 Maven-3.0.3Eclipse请阅读者事先具备一定maven知识 2 配置2.1 添加Jetty插件 在pom.xml中加入如下代码段 <plugin> <gr ...
- wget https://github.com/xxx/yyy/archive/${commit_hash}.zip
wget https://github.com/xxx/yyy/archive/${commit_hash}.zip