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. Centos 用户组管理

    #组帐号管理 linux 组管理 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--= 1.组的分类 私用组:只能包含一个用 ...

  2. jquery的extend和fn.extend的使用说明

    jQuery.fn.extend(object); 对jQuery.prototype进得扩展,就是为jQuery类添加“成员函数”.jQuery类的实例可以使用这个“成员函数”. jQuery为开发 ...

  3. 编辑器Emacs下载网址(中国镜像)

      Root gnu emacs windows File Name ↓ File Size ↓ Date ↓ Parent directory/ - - README 14K 2014-Nov-15 ...

  4. android AsyncTask异步下载并更新进度条

    AsyncTask异步下载并更新进度条    //如果不是很明白请看上篇文章的异步下载 AsyncTask<String, Integer, String> 第一个参数:String 传入 ...

  5. linux ps top 命令 VSZ,RSS,TTY,STAT, VIRT,RES,SHR,DATA的含义【转】

    转自:http://blog.csdn.net/zjc156m/article/details/38920321 http://javawind.net/p131 VIRT:virtual memor ...

  6. (七)后台.apsx.cs获取前台客户端文本框的内容

    <input ID='AllLocalData' name='AllLocalDataName' /> 其中最重要的一点是Request.Form[]中括号是放的name属性而非Id属性. ...

  7. Eclipse 下如何引用另一个项目的资源文件

    为什么要这么做?可参考:Eclipse 下如何引用另一个项目的Java文件 下面直接说下步骤:(项目A 引用 项目B的资源文件) 1.右键 项目A,点击菜单 Properties 2.在弹出的框中,点 ...

  8. iReport —— 使用 JavaBean 作为数据源

    在制作报表时,想直接使用Java代码提供数据. 网上找了一些文章,很多都是用Servlet做的.我不是想通过浏览器来观察它的输出.我想使用iReport的动态连接直接预览. 结合一些资料,加上自己的摸 ...

  9. [原]Water Water Union-Find Set &amp; Min-Spanning Tree Problems&#39; Set~Orz【updating...】

    [HDU] 1213 - How Many Tables [基础并查集,求父节点个数] 1856 -More is better [基础并查集,注意内存,HDU数据水了,不用离散化,注意路径压缩的方式 ...

  10. How Uuencoding Works

    做题目学习  https://www.zhihu.com/question/26598476/answer/45396765 http://email.about.com/od/emailbehind ...