用android代码显示图片的一部分源码
ShowPoritionPictureActivity代码:
[java] <span style="font-size:16px;">
package com.iwin.zzs;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.WindowManager;
public class ShowPoritionPictureActivity extends Activity {
/** Called when the activity is first created. */
Bitmap picRes;
Bitmap showPic;
//获取原图片的宽和高
int picWidth;
int picHeight;
private PoritionView poritonView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 不显示状态栏
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
// 得到屏幕的长和宽
int screenWidth = dm.widthPixels; //水平分辨率
int screenHeight = dm.heightPixels; //垂直分辨率
picRes = BitmapFactory.decodeResource(this.getResources(), R.drawable.girl);
// 得到图片的长和宽
picWidth = picRes.getWidth();
picHeight = picRes.getHeight();
// 计算缩放率,新尺寸除原始尺寸
float scaleWidth = ((float) screenWidth ) / picWidth;
float scaleHeight = ((float) screenHeight ) / picHeight;
// 创建操作图片用的matrix对象
Matrix matrix = new Matrix();
// 缩放图片动作
matrix.postScale(scaleWidth, scaleHeight);
// 新得到的图片是原图片经过变换填充到整个屏幕的图片
Bitmap picNewRes = Bitmap.createBitmap(picRes, 0, 0,picWidth, picHeight, matrix, true);
// bitmap = Bitmap.createBitmap(400, 480, Bitmap.Config.ARGB_8888);
// canvas=new Canvas();
// canvas.setBitmap(bitmap);
showPic = Bitmap.createBitmap(picNewRes, screenWidth/2-50, screenHeight/2-50, 100, 100);
poritonView = new PoritionView(this);
poritonView.setBitmapShow(showPic, screenWidth/2-50, screenHeight/2-50);
setContentView(poritonView);
}
</span>
<span style="font-size:16px;">package com.iwin.zzs;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.WindowManager;
public class ShowPoritionPictureActivity extends Activity {
/** Called when the activity is first created. */
Bitmap picRes;
Bitmap showPic;
//获取原图片的宽和高
int picWidth;
int picHeight;
private PoritionView poritonView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 不显示状态栏
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
// 得到屏幕的长和宽
int screenWidth = dm.widthPixels; //水平分辨率
int screenHeight = dm.heightPixels; //垂直分辨率
picRes = BitmapFactory.decodeResource(this.getResources(), R.drawable.girl);
// 得到图片的长和宽 www.2cto.com
picWidth = picRes.getWidth();
picHeight = picRes.getHeight();
// 计算缩放率,新尺寸除原始尺寸
float scaleWidth = ((float) screenWidth ) / picWidth;
float scaleHeight = ((float) screenHeight ) / picHeight;
// 创建操作图片用的matrix对象
Matrix matrix = new Matrix();
// 缩放图片动作
matrix.postScale(scaleWidth, scaleHeight);
// 新得到的图片是原图片经过变换填充到整个屏幕的图片
Bitmap picNewRes = Bitmap.createBitmap(picRes, 0, 0,picWidth, picHeight, matrix, true);
// bitmap = Bitmap.createBitmap(400, 480, Bitmap.Config.ARGB_8888);
// canvas=new Canvas();
// canvas.setBitmap(bitmap);
showPic = Bitmap.createBitmap(picNewRes, screenWidth/2-50, screenHeight/2-50, 100, 100);
poritonView = new PoritionView(this);
poritonView.setBitmapShow(showPic, screenWidth/2-50, screenHeight/2-50);
setContentView(poritonView);
}
</span>
PoritionView类代码:
[java] <span style="font-size:16px;">package com.iwin.zzs;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.View;
public class PoritionView extends View {
private Bitmap showPic = null;
private int startX = 0;
private int startY = 0;
public PoritionView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(showPic, startX, startY, null);
}
public void setBitmapShow(Bitmap b, int x, int y)
{
showPic = b;
startX = x;
startY = y;
}
}
</span>
<span style="font-size:16px;">package com.iwin.zzs;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.View;
public class PoritionView extends View {
private Bitmap showPic = null;
private int startX = 0;
private int startY = 0;
public PoritionView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(showPic, startX, startY, null);
}
public void setBitmapShow(Bitmap b, int x, int y)
{
showPic = b;
startX = x;
startY = y;
}
}
</span>
在工程res/drawable里面添加图片gir.png,运行程序的效果是只显示图片的中间100*100的那部分图片。
用android代码显示图片的一部分源码的更多相关文章
- JAVA 显示图片的简单源码 分类: Java Game 2014-08-14 10:10 77人阅读 评论(0) 收藏
此代码的原理就是用JLabel来加载图片,再将JLabel放入JFrame中, package com.mywork; import javax.swing.ImageIcon; import jav ...
- 50个Android开发人员必备UI效果源码[转载]
50个Android开发人员必备UI效果源码[转载] http://blog.csdn.net/qq1059458376/article/details/8145497 Android 仿微信之主页面 ...
- [转载] 50个Android开发人员必备UI效果源码
好东西,多学习! Android 仿微信之主页面实现篇Android 仿微信之界面导航篇Android 高仿QQ 好友分组列表Android 高仿QQ 界面滑动效果Android 高仿QQ 登陆界面A ...
- 黎活明8天快速掌握android视频教程--24_网络通信之网页源码查看器
1 该项目的主要功能就是从将后台的html网页在Android的界面上显示出来 后台就是建立一个java web工程在工程尚建立一个html或者jsp文件就可以了,这里主要看Android客户端的程序 ...
- Android 网络图片查看器与网页源码查看器
在AndroidManifest.xml里面先添加访问网络的权限: <uses-permission android:name="android.permission.INTERNET ...
- 【原】Android热更新开源项目Tinker源码解析系列之三:so热更新
本系列将从以下三个方面对Tinker进行源码解析: Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Android热更新开源项目Tinker源码解析系列之二:资源文件热更新 A ...
- 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新
[原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...
- Android进阶:五、RxJava2源码解析 2
上一篇文章Android进阶:四.RxJava2 源码解析 1里我们讲到Rxjava2 从创建一个事件到事件被观察的过程原理,这篇文章我们讲Rxjava2中链式调用的原理.本文不讲用法,仍然需要读者熟 ...
- 编译Android 4.4.4 r1的源码刷Nexus 5手机详细教程
本文博客地址:http://blog.csdn.net/qq1084283172/article/details/54562606 网上关于编译Android源码的教程已经很多了,但是讲怎么编译And ...
随机推荐
- CocoaPods在使用中的几个问题
来源: http://blog.cocoapods.org/Repairing-Our-Broken-Specs-Repository/ 1. 当把CocoaPods生成的workspace移动到上层 ...
- Pointers to classes (From the note of my firend)
Pointers to classes Objects can also be pointed to by pointers: Once declared, a class becomes a ...
- javascript3
计算阶乘函数:<script> function factorial(n){ var product=1; while (n>1){ product*=n;//product=pro ...
- effective C++ 札记 规定17 在单独的陈述newed对象插入智能指针
// Test.cpp : 自定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include &l ...
- 更改MYSQL数据库不区分大小写表名
今天郁闷死了,在LINUX下调一个程序老说找不到表,但是我明明是建了表的,在MYSQL的命令行下也可以查到,为什么程序就找不到表呢? 后来请教了一个老师才搞定,原来是LINUX下的MYSQL默认是要区 ...
- PHP中将内容循环出来
首先连接数据库: $myDate= @mysql_connect("localhost","root","") or die("数 ...
- ASP.NET MVC应用程序使用axd格式文件
ASP.NET MVC应用程序使用axd格式文件 axd格式文件,不管是在asp.net还是现在开发asp.net MVC应用程序,都是Insus.NET较喜欢使用的. 因为我们可以虚拟一个在应用程序 ...
- javascript-无间缝滚动,封装
原生javascript-无间缝滚动,封装 目前支持的是竖向与横向滚动 http://lgyweb.com/marScroll/ 现在分析下无间缝实现的基本思路(竖向例子): HTML结构: 1 &l ...
- Remote验证及其改进(附源码)
Remote验证及其改进(附源码) 表单中的输入项,有些是固定的,不变的验证规则,比如字符长度,必填等.但有些是动态的,比如注册用户名是否存在这样的检查,这个需要访问服务器后台才能解决.这篇文章将会介 ...
- linux时间校准设置,解决与本地时间不一致问题
时间安装脚本 从NTP上把时间同步到本地 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 更新本地时间 ntpdate us.pool.nt ...