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实现图片的绘制、旋转、缩放的更多相关文章

  1. 安卓自定义View实现图片上传进度显示(仿QQ)

    首先看下我们想要实现的效果如下图(qq聊天中发送图片时的效果): 再看下图我们实现的效果: 实现原理很简单,首先我们上传图片时需要一个进度值progress,这个不管是自己写的上传的方法还是使用第三方 ...

  2. 自定义View实现图片热区效果

    我司主要从事工业物联网领域软件的开发,现有个需求,在外废品处理时需要对产品的不良位置进行标记,点选图片实现图片网格的着色功能. 需求是通过自定义view来实现,实现思路如下: 首先将点击的小方格对象实 ...

  3. Android -- 自定义View小Demo,绘制四位数随机码(一)

    1,现在有这样一个需求,实现显示随机随机数可能在代码中直接很简单的就实现了,但是现在我们直接自定义View来实现这个效果,那么我们来分析一波吧,我们允许开发者自己设置这个textview的大小,颜色, ...

  4. Android -- 自定义View小Demo,绘制钟表时间(一)

    1,昨天刚看了hongyang大神推荐的自定义时钟效果(传动门:http://www.jianshu.com/users/a45d19d680af/),效果还是不错的,自己又在github上找了找,发 ...

  5. android 缩放平移自定义View 显示图片

    1.背景 现在app中,图片预览功能肯定是少不了的,用户基本已经形成条件反射,看到小图,点击看大图,看到大图两个手指开始进行放大,放大后,开始移动到指定部位~~~ 我相信看图的整个步骤,大家或者说用户 ...

  6. 自定义view(14)使用Path绘制复杂图形

    灵活使用 Path ,可以画出复杂图形,就像美术生在画板上画复杂图形一样.程序员也可以用代码实现. 1.样板图片 这个是个温度计,它是静态的,温度值是动态变化的,所以要自定义个view.动态显示值,温 ...

  7. Android自定义View——QQ音乐中圆形旋转碟子

    1.在onMeasure中测量整个View的宽和高后,设置宽高 2.获取我们res的图片资源后,在ondraw方法中进行绘制圆形图片 3.通过Handler发送Runnable在主线程中更新UI,达到 ...

  8. Android 自定义 View 圆形进度条总结

    Android 自定义圆形进度条总结 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 微信公众号:牙锅子 源码:CircleProgress 文中如有纰漏,欢迎大家留言指出. 最近 ...

  9. 安卓自定义View教程目录

    基础篇 安卓自定义View基础 - 坐标系 安卓自定义View基础 - 角度弧度 安卓自定义View基础 - 颜色 进阶篇 安卓自定义View进阶 - 分类和流程 安卓自定义View进阶 - Canv ...

随机推荐

  1. 305. Number of Islands II

    题目: A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand  ...

  2. Myeclipse 10 破解说明

    一,准备阶段 : 1. 破解软件(网上有下载) 2. JDK软件(免费软件) 二,开始破解: 1. 关闭MyEclipse 10.0 2. 安装 JDK 三,特殊说明: 有些机器安装时会出现run.b ...

  3. Android应用截图和SurfaceView截图问题总结

          最近在做android截图应用的过程遇到很多问题,接触了好些截图方法,但是还是不能实现SufaceView截图功能.今天就把我尝试过的方法总结下,希望把我惨痛的经历写出来后能够帮助到要做此 ...

  4. 高性能jdbc封装工具 Apache Commons DbUtils 1.6(转载)

    转载自原文地址:http://gao-xianglong.iteye.com/blog/2166444 前言 关于Apache的DbUtils中间件或许了解的人并不多,大部分开发人员在生成环境中更多的 ...

  5. [转]设置控件全局显示样式appearance proxy

    转自:huifeidexin_1的专栏 appearance是apple在iOS5.0上加的一个协议,它让程序员可以很轻松地改变某控件的全局样式(背景) @selector(appearance) 支 ...

  6. 转: $GLOBALS['HTTP_RAW_POST_DATA'] 和$_POST的区别

    $_POST:通过 HTTP POST 方法传递的变量组成的数组.是自动全局变量. $GLOBALS['HTTP_RAW_POST_DATA'] :总是产生 $HTTP_RAW_POST_DATA 变 ...

  7. leetcode:Count Primes

    Description:Count the number of prime numbers less than a non-negative number, n. 本题给定一个非负数n,让我们求小于n ...

  8. 标准模板库(STL)学习探究之stack

    标准模板库(STL)学习探究之stack queue priority_queue list map/multimap dequeue string

  9. HTTP头学习汇总

    在开发http请求的时候,对HTTP头部信息一知半解,各种百度谷歌汇总一下学习到的资料.   http简介 HTTP(HyperTextTransferProtocol)是超文本传输协议的缩写,它用于 ...

  10. Android 第三方应用接入微信平台(1)

    关键字:微信开放平台   Android第三方应用接入微信 微信平台开放后倒是挺火的,许多第三方应用都想试下接入微信这个平台, 毕竟可以利用微信建立起来的关系链来拓展自己的应用还是挺不错的,可 以节约 ...