bitmap-setBounds方法参数研究
对于如下的代码,一直有点不明白它具体每一步操作的影响。今天就稍微研究下。代码如下
xml代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:internal="http://schemas.android.com/apk/prv/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#555555"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test1.MainActivity" > <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
internal:layout_maxHeight="20dp"
internal:layout_maxWidth="100dp"
internal:layout_minHeight="20dp"
android:orientation="vertical" > <ImageButton
android:id="@+id/tv"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#0000ff" /> <ImageButton
android:id="@+id/tv2"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="20dp"
android:background="#ff0000" /> <ImageButton
android:id="@+id/tv3"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="20dp"
android:background="#ff0000" />
</LinearLayout> </RelativeLayout>
activity中的代码如下
package com.example.testcode; import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;
import android.widget.ImageView; public class MainActivity extends Activity {
ImageButton tView;
ImageButton tView2;
ImageButton tView3; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tView = (ImageButton) findViewById(R.id.tv);
tView3 = (ImageButton) findViewById(R.id.tv3);
Drawable drawable = getResources().getDrawable(R.drawable.test);
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
Log.e("123", "drawable:w==" + w + "|||" + "drawable:h===" + h);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
w = bitmap.getWidth();
h = bitmap.getHeight();
Log.e("123", "bitmap:w==" + w + "|||" + "bitmap:h===" + h); tView.setImageDrawable(new BitmapDrawable(getResources(), bitmap));
tView3.setImageDrawable(getResources().getDrawable(
R.drawable.test));
tView2 = (ImageButton) findViewById(R.id.tv2);
Drawable drawable2 = getResources().getDrawable(R.drawable.test);
int w2 = drawable2.getIntrinsicWidth();
int h2 = drawable2.getIntrinsicHeight();
Log.e("123", "drawable2:w2==" + w2 + "|||" + "drawable2:h2===" + h2);
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
Bitmap bitmap2 = Bitmap.createBitmap(150, 150, config);
w2 = bitmap2.getWidth();
h2 = bitmap2.getHeight();
Log.e("123", "bitmap2:w2==" + w2 + "|||" + "bitmap2:h2===" + h2);
Canvas canvas = new Canvas(bitmap2);
drawable2.setBounds(50, 50, w2 + 50, h2 + 50);
drawable2.draw(canvas);
w2 = drawable2.getIntrinsicWidth();
h2 = drawable2.getIntrinsicHeight();
Log.e("123", "drawable3:w2==" + w2 + "|||" + "drawable3:h2===" + h2);
tView2.setImageDrawable(new BitmapDrawable(getResources(), bitmap2));
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
图片

1)对于上面的代码,打印log如下
1 01-01 09:12:10.867: E/123(3373): drawable:w==174|||drawable:h===174
2 01-01 09:12:10.867: E/123(3373): bitmap:w==174|||bitmap:h===174
3 01-01 09:12:10.869: E/123(3373): drawable2:w2==174|||drawable2:h2===174
4 01-01 09:12:10.870: E/123(3373): bitmap2:w2==150|||bitmap2:h2===150
5 01-01 09:12:10.872: E/123(3373): drawable3:w2==174|||drawable3:h2===174
效果图如下

2)现在我们研究
drawable2.setBounds(0, 0, w2, h2);
Bitmap bitmap2 = Bitmap.createBitmap(150, 150, config);
这个属性的影响
修改1
Bitmap bitmap2 = Bitmap.createBitmap(200, 200, config);
drawable2.setBounds(0, 0, w2, h2);
结果
1 01-01 09:16:18.529: E/123(3986): drawable:w==174|||drawable:h===174
2 01-01 09:16:18.530: E/123(3986): bitmap:w==174|||bitmap:h===174
3 01-01 09:16:18.531: E/123(3986): drawable2:w2==174|||drawable2:h2===174
4 01-01 09:16:18.532: E/123(3986): bitmap2:w2==200|||bitmap2:h2===200
5 01-01 09:16:18.536: E/123(3986): drawable3:w2==174|||drawable3:h2===174

修改2
drawable2.setBounds(100, 100, w2, h2);
结果
1 01-01 09:16:18.529: E/123(3986): drawable:w==174|||drawable:h===174
2 01-01 09:16:18.530: E/123(3986): bitmap:w==174|||bitmap:h===174
3 01-01 09:16:18.531: E/123(3986): drawable2:w2==174|||drawable2:h2===174
4 01-01 09:16:18.532: E/123(3986): bitmap2:w2==200|||bitmap2:h2===200
5 01-01 09:16:18.536: E/123(3986): drawable3:w2==174|||drawable3:h2===174

现在证明了,这个方法,对于原来的图片是没有任何的影响的。
修改3
drawable2.setBounds(100, 100, 99, 99);

修改4)
drawable2.setBounds(100, 100, 120, 120);

修改5)
drawable2.setBounds(100, 100, 200, 200);

修改7)
drawable2.setBounds(100, 100, 220, 220);

结论明了了
1.createBitmap(w,h,config)的w,h决定了一个矩形区域。我们的setbounds 的显示区域就在这里
2.setbounds(left,top,right,bottom),它是在bitmap的区域里面画一个矩形,我们的drawable就显示在这个矩形里。
这个矩形的正对角线端点坐标为(left,top),(right,bottom)

3.我们图形的可见区域就是这两个矩形的交集。所以说,
(1)如果我们的bound矩形全部可见的话,right>left,bottom>left,而且这个矩形整个的都要在bitmap矩形内
(2)如果bound矩形超出了bitmap矩形,那么超出部分就不会再显示了
bitmap-setBounds方法参数研究的更多相关文章
- Java基础学习笔记十二 类、抽象类、接口作为方法参数和返回值以及常用API
不同修饰符使用细节 常用来修饰类.方法.变量的修饰符 public 权限修饰符,公共访问, 类,方法,成员变量 protected 权限修饰符,受保护访问, 方法,成员变量 默认什么也不写 也是一种权 ...
- Python进阶----反射(四个方法),函数vs方法(模块types 与 instance()方法校验 ),双下方法的研究
Python进阶----反射(四个方法),函数vs方法(模块types 与 instance()方法校验 ),双下方法的研究 一丶反射 什么是反射: 反射的概念是由Smith在1982年首次提出的 ...
- JAVA8初探-让方法参数具备行为能力并引入Lambda表达式
关于JAVA8学习的意义先来贴一下某网站上的对它的简单介绍:“Java 8可谓Java语言历史上变化最大的一个版本,其承诺要调整Java编程向着函数式风格迈进,这有助于编写出更为简洁.表达力更强,并且 ...
- 关于PHP的方法参数类型约束
在之前的文章PHP方法参数的那点事儿中,我们讲过关于PHP方法参数的一些小技巧.今天,我们带来的是更加深入的研究一下PHP中方法的参数类型. 在PHP5之后,PHP正式引入了方法参数类型约束.也就是如 ...
- 【.net 深呼吸】细说CodeDom(6):方法参数
本文老周就给大伙伴们介绍一下方法参数代码的生成. 在开始之前,先补充一下上一篇烂文的内容.在上一篇文章中,老周检讨了 MemberAttributes 枚举的用法,老周此前误以为该枚举不能进行按位操作 ...
- Eclipse中自动提示的方法参数都是arg0,arg1的解决方法
Eclipse中自动提示的方法参数都是arg0,arg1,就不能根据参数名来推断参数的含义,非常不方便. 解决方法:Preferences->Java->Installed JREs,发现 ...
- java中方法参数的一些总结(1)
1.问题说明 在C++中,函数调用时有传值调用和传址调用两种方式,但在Java中只有传值调用一种方式.Java中的方法参数为那几种基本数据类型的情况跟C++中一样,传入的只是变量的拷贝. ...
- php课程---Windows.open()方法参数详解
Window.open()方法参数详解 1, 最基本的弹出窗口代码 window.open('page.html'); 2, 经过设置后的弹出窗口 window.open('page.html ...
- Scala正则和抽取器:解析方法参数
在<正则表达式基础知识>中概括了正则表达式的基础知识, 本文讲解如何使用正则表达式解析方法参数,从而可以根据 DAO 自动生成 Service. 在做 Java 项目时,常常要根据 DAO ...
随机推荐
- Programming Languages - Coursera 整理
找到并学习这门课的原因: 想要学习 functional programming Week1 Introduction and Course-Wide Information week1 很轻松, 主 ...
- Linux桌面词典 星际译王(StarDict)
星际译王(StarDict)是利用GTK(GIMP TOOLKIT)开发的国际化的.跨平台的自由的桌面字典软件.它并不包含字典档,使用者须自行下载配合使用.它可以运行于多种不同的平台,如Linux, ...
- 【Linux下自定义Shell终端提示符】
目录 基本转义符 字体颜色 背景颜色 移动光标 @ Linux系统终端提示符的特征由系统环境变量 PS1(Prompt String One)定义. 我们可以通过命令echo $PS1来查看当前设置, ...
- Mark Compact GC (Part one: Lisp2)
目录 什么是GC 标记-压缩算法 Lisp2 算法的对象 概要 步骤 步骤一:设定forwarding指针 步骤二:更新指针 步骤三:移动对象 优缺点 什么是GC 标记-压缩算法 需要对标记清除和GC ...
- RvmTranslator6.0 - AVEVA Marine Hull Model
eryar@163.com 1. Introduction RvmTranslator can translate the RVM file exported by AVEVA Plant(PDMS) ...
- [易飞]一张领料单单身仓库"飞了"引起的思考
[摘要]我司每月月初10号前財务要出报表.故10号前要做完毕本月结. PMC经理接到仓管员反馈. 物料-30408011003在账上怎么还有那么多?上次发料的时候已发完. 相应的领料单:5403-20 ...
- java一个月日历
项目须要,获取当天之后的30天.并提示星期几(周几),写了一个工具类 /** * 计算日期时间 * @author shijing * 2015年8月10日下午2:16:09 * @param dat ...
- xml中控件调用构造方法
系统控件view有三个构造方法,分别如下 public DrawView(Context context) { super(context); } public DrawView(Context co ...
- C#List<string>和string[]之间的相互转换
一.LIST概述 所属命名空间:System.Collections.Generic public class List<T> : IList<T>, IColle ...
- ocdefirst安装更新数据库
数据迁移(Migrations) 启用数据迁移在控制台中输入如下命令:Enable-Migrations 这时会在项目目录中增加一个Migrations文件夹,里面放置了两个文件:EF会通过C#代码的 ...