- 浏览: 3904402 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
hinuliba:
...
字体背景颜色的高度修改 -
KANGOD:
最后的 -createDialog() 私有方法是怎么回事,没 ...
简单的实现listView中item多个控件以及点击事件 -
sswangqiao:
呵呵,呵呵
onActivityResult传值的使用 -
yumeiqiao:
感觉你所的不清楚 lstView.setOnTouchLi ...
listview中viewflipper的问题 -
lizhou828:
果然是大神啊!!!
Animation动画效果的实现
在DensityActivity这个历程中,主要讲述了动态添加view的方法,而其中包含的东西是比较琐碎的。
view.setBackgroundDrawable(d);其中d可以为Drawable也可以为BitmapDrawable
所以实现方法就有两种
Drawable d = getResources().getDrawable(resource);
第二种呢 :
bitmap = BitmapFactory.decodeResource(getResources(), id);
BitmapDrawable d = new BitmapDrawable(getResources(), bitmap);
其实 也就实现了Bitmap到Drawable的转化。
下面也有两个添加view
private void addLabelToRoot(LinearLayout root, String text) {
TextView label = new TextView(this);
label.setText(text);
root.addView(label, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
}
和
private void addResourceDrawable(LinearLayout layout, int resource) {
View view = new View(this);
final Drawable d = getResources().getDrawable(resource);
view.setBackgroundDrawable(d);
view.setLayoutParams(new LinearLayout.LayoutParams(d.getIntrinsicWidth(),
d.getIntrinsicHeight()));
layout.addView(view);
}
主要用到了layout.addView和view.setLayoutParams
这都是从代码中添加的,这里还提到了一种从xml中添加view的方法,这中方法有分两种:
LayoutInflater li = (LayoutInflater)getSystemService(
LAYOUT_INFLATER_SERVICE);
layout = (LinearLayout)li.inflate(R.layout.density_image_views, null); //返回一个在xml中已经布局好的试图
density_image_views 代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/reslogo120dpi" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/reslogo160dpi" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/reslogo240dpi" />
</LinearLayout>
另一种形式:
layout = (LinearLayout)li.inflate(R.layout.density_styled_image_views, null);
density_styled_image_views的代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView style="@style/ImageView120dpi" />
<ImageView style="@style/ImageView160dpi" />
<ImageView style="@style/ImageView240dpi" />
</LinearLayout>
style下的代码如下:
<style name="ImageView120dpi">
<item name="android:src">@drawable/stylogo120dpi</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="ImageView160dpi">
<item name="android:src">@drawable/stylogo160dpi</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="ImageView240dpi">
<item name="android:src">@drawable/stylogo240dpi</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
</resources>
其实两种方式从本质上来说都是一样的。
而在这个实例中最后还讲述了onMeasure的应用
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final DisplayMetrics metrics = getResources().getDisplayMetrics();
setMeasuredDimension(
mBitmap.getScaledWidth(metrics),
mBitmap.getScaledHeight(metrics));
}
默认的onMeasure提供的大小是100*100所以你想设置自己view的大小 必须实现此方法它的意思是 父view问你 你要多大的空间啊?然后 需要自己计算你需要的大小
然后在必须实现setMeasuredDimension 来通知你设置试图的大小
这里呢 你可以参看:
http://www.chinaup.org/docs/toolbox/custom-components.html
http://www.cnblogs.com/xirihanlin/archive/2009/07/23/1529111.html
http://www.cnblogs.com/xirihanlin/archive/2009/07/23/1529238.html
除非你总是需要一个100×100像素的控件,否则,你必须要重写onMeasure。
onMeasure方法在控件的父元素正要放置它的子控件时调用。它会问一个问题,“你想要用多大地方啊?”,然后传入两个参数——
widthMeasureSpec和heightMeasureSpec。它们指明控件可获得的空间以及关于这个空间描述的元数据。
比返回一个结果要好的方法是你传递View的高度和宽度到setMeasuredDimension方法里。
接下来的代码片段给出了如何重写onMeasure。注意,调用的本地空方法是来计算高度和宽度的。它们会译解widthHeightSpec和heightMeasureSpec值,并计算出合适的高度和宽度值。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int measuredHeight = measureHeight(heightMeasureSpec);
int measuredWidth = measureWidth(widthMeasureSpec);
setMeasuredDimension(measuredHeight, measuredWidth);
}
private int measureHeight(int measureSpec) {
// Return measured widget height.
}
private int measureWidth(int measureSpec) {
// Return measured widget width.
}
边界参数——widthMeasureSpec和heightMeasureSpec ,效率的原因以整数的方式传入。在它们使用之前,首先要做的是使用MeasureSpec类的静态方法getMode和getSize来译解,如下面的片段所示:
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
依据specMode的值,如果是AT_MOST,specSize 代表的是最大可获得的空间;如果是EXACTLY,specSize 代表的是精确的尺寸;如果是UNSPECIFIED,对于控件尺寸来说,没有任何参考意义。
当以EXACT方式标记测量尺寸,父元素会坚持在一个指定的精确尺寸区域放置View。在父元素问子元素要多大空间时,AT_MOST指示者会说给我最大的范围。在很多情况下,你得到的值都是相同的。
在两种情况下,你必须绝对的处理这些限制。在一些情况下,它可能会返回超出这些限制的尺寸,在这种情况下,你可以让父元素选择如何对待超出的View,使用裁剪还是滚动等技术。
接下来的框架代码给出了处理View测量的典型实现:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int measuredHeight = measureHeight(heightMeasureSpec);
int measuredWidth = measureWidth(widthMeasureSpec);
setMeasuredDimension(measuredHeight, measuredWidth);
}
private int measureHeight(int measureSpec) {
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
// Default size if no limits are specified.
int result = 500;
if (specMode == MeasureSpec.AT_MOST)
{
// Calculate the ideal size of your
// control within this maximum size.
// If your control fills the available
// space return the outer bound.
result = specSize;
}
else if (specMode == MeasureSpec.EXACTLY)
{
// If your control can fit within these bounds return that value.
result = specSize;
}
return result;
}
private int measureWidth(int measureSpec) {
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
// Default size if no limits are specified.
int result = 500;
if (specMode == MeasureSpec.AT_MOST)
{
// Calculate the ideal size of your control
// within this maximum size.
// If your control fills the available space
// return the outer bound.
result = specSize;
}
else if (specMode == MeasureSpec.EXACTLY)
{
// If your control can fit within these bounds return that value.
result = specSize;
}
return result;
}
view.setBackgroundDrawable(d);其中d可以为Drawable也可以为BitmapDrawable
所以实现方法就有两种
Drawable d = getResources().getDrawable(resource);
第二种呢 :
bitmap = BitmapFactory.decodeResource(getResources(), id);
BitmapDrawable d = new BitmapDrawable(getResources(), bitmap);
其实 也就实现了Bitmap到Drawable的转化。
下面也有两个添加view
private void addLabelToRoot(LinearLayout root, String text) {
TextView label = new TextView(this);
label.setText(text);
root.addView(label, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
}
和
private void addResourceDrawable(LinearLayout layout, int resource) {
View view = new View(this);
final Drawable d = getResources().getDrawable(resource);
view.setBackgroundDrawable(d);
view.setLayoutParams(new LinearLayout.LayoutParams(d.getIntrinsicWidth(),
d.getIntrinsicHeight()));
layout.addView(view);
}
主要用到了layout.addView和view.setLayoutParams
这都是从代码中添加的,这里还提到了一种从xml中添加view的方法,这中方法有分两种:
LayoutInflater li = (LayoutInflater)getSystemService(
LAYOUT_INFLATER_SERVICE);
layout = (LinearLayout)li.inflate(R.layout.density_image_views, null); //返回一个在xml中已经布局好的试图
density_image_views 代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/reslogo120dpi" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/reslogo160dpi" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/reslogo240dpi" />
</LinearLayout>
另一种形式:
layout = (LinearLayout)li.inflate(R.layout.density_styled_image_views, null);
density_styled_image_views的代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView style="@style/ImageView120dpi" />
<ImageView style="@style/ImageView160dpi" />
<ImageView style="@style/ImageView240dpi" />
</LinearLayout>
style下的代码如下:
<style name="ImageView120dpi">
<item name="android:src">@drawable/stylogo120dpi</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="ImageView160dpi">
<item name="android:src">@drawable/stylogo160dpi</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="ImageView240dpi">
<item name="android:src">@drawable/stylogo240dpi</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
</resources>
其实两种方式从本质上来说都是一样的。
而在这个实例中最后还讲述了onMeasure的应用
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final DisplayMetrics metrics = getResources().getDisplayMetrics();
setMeasuredDimension(
mBitmap.getScaledWidth(metrics),
mBitmap.getScaledHeight(metrics));
}
默认的onMeasure提供的大小是100*100所以你想设置自己view的大小 必须实现此方法它的意思是 父view问你 你要多大的空间啊?然后 需要自己计算你需要的大小
然后在必须实现setMeasuredDimension 来通知你设置试图的大小
这里呢 你可以参看:
http://www.chinaup.org/docs/toolbox/custom-components.html
http://www.cnblogs.com/xirihanlin/archive/2009/07/23/1529111.html
http://www.cnblogs.com/xirihanlin/archive/2009/07/23/1529238.html
除非你总是需要一个100×100像素的控件,否则,你必须要重写onMeasure。
onMeasure方法在控件的父元素正要放置它的子控件时调用。它会问一个问题,“你想要用多大地方啊?”,然后传入两个参数——
widthMeasureSpec和heightMeasureSpec。它们指明控件可获得的空间以及关于这个空间描述的元数据。
比返回一个结果要好的方法是你传递View的高度和宽度到setMeasuredDimension方法里。
接下来的代码片段给出了如何重写onMeasure。注意,调用的本地空方法是来计算高度和宽度的。它们会译解widthHeightSpec和heightMeasureSpec值,并计算出合适的高度和宽度值。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int measuredHeight = measureHeight(heightMeasureSpec);
int measuredWidth = measureWidth(widthMeasureSpec);
setMeasuredDimension(measuredHeight, measuredWidth);
}
private int measureHeight(int measureSpec) {
// Return measured widget height.
}
private int measureWidth(int measureSpec) {
// Return measured widget width.
}
边界参数——widthMeasureSpec和heightMeasureSpec ,效率的原因以整数的方式传入。在它们使用之前,首先要做的是使用MeasureSpec类的静态方法getMode和getSize来译解,如下面的片段所示:
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
依据specMode的值,如果是AT_MOST,specSize 代表的是最大可获得的空间;如果是EXACTLY,specSize 代表的是精确的尺寸;如果是UNSPECIFIED,对于控件尺寸来说,没有任何参考意义。
当以EXACT方式标记测量尺寸,父元素会坚持在一个指定的精确尺寸区域放置View。在父元素问子元素要多大空间时,AT_MOST指示者会说给我最大的范围。在很多情况下,你得到的值都是相同的。
在两种情况下,你必须绝对的处理这些限制。在一些情况下,它可能会返回超出这些限制的尺寸,在这种情况下,你可以让父元素选择如何对待超出的View,使用裁剪还是滚动等技术。
接下来的框架代码给出了处理View测量的典型实现:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int measuredHeight = measureHeight(heightMeasureSpec);
int measuredWidth = measureWidth(widthMeasureSpec);
setMeasuredDimension(measuredHeight, measuredWidth);
}
private int measureHeight(int measureSpec) {
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
// Default size if no limits are specified.
int result = 500;
if (specMode == MeasureSpec.AT_MOST)
{
// Calculate the ideal size of your
// control within this maximum size.
// If your control fills the available
// space return the outer bound.
result = specSize;
}
else if (specMode == MeasureSpec.EXACTLY)
{
// If your control can fit within these bounds return that value.
result = specSize;
}
return result;
}
private int measureWidth(int measureSpec) {
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
// Default size if no limits are specified.
int result = 500;
if (specMode == MeasureSpec.AT_MOST)
{
// Calculate the ideal size of your control
// within this maximum size.
// If your control fills the available space
// return the outer bound.
result = specSize;
}
else if (specMode == MeasureSpec.EXACTLY)
{
// If your control can fit within these bounds return that value.
result = specSize;
}
return result;
}
发表评论
-
URI 转path
2019-06-26 10:41 1230转自知乎Matisse package com.zhihu ... -
权限申请
2017-09-22 13:25 1226public class PermissionActivit ... -
onPreviewFrame 相机输出格式转换yuv420p保存成图片
2015-11-25 15:59 7488在最近项目中,因为特殊需要,底层相机往外输出了i420 也 ... -
new Android's Runtime Permission
2015-11-03 21:05 1194targetSdkVersion 23 开始 使用运行时权 ... -
自定义listview 边缘效果
2015-02-28 10:58 1703static void ChangeEdgeEffect( ... -
发射打开wifi
2015-01-07 10:25 1376WifiManager wifiManager = (Wif ... -
RecyclerView
2014-11-05 13:08 1215http://www.grokkingandroid.com ... -
获取点击区域
2014-04-28 09:39 1522@Override public void getHitR ... -
speex 和libogg 编译
2014-04-03 16:17 6370下载: http://www.speex.org/down ... -
rsync 同步
2014-03-28 17:06 1802两台android 设备 进行rsy ... -
流转字符串
2014-03-11 09:49 1479public static String convertSt ... -
java simplexml 序列化
2014-03-06 13:22 5886<?xml version="1.0&quo ... -
获取其他程序的特定资源
2014-03-05 09:33 1659try { PackageManager man ... -
检测来电属于哪个sim卡
2014-02-07 10:41 1676public class IncomingCallInter ... -
使用 NDK r9 编译ffmpeg
2014-01-16 13:32 167701. 环境 ubuntu 我的是13.10 ndk r9 ... -
android h264含so
2014-01-13 11:24 1510http://download.csdn.net/downlo ... -
xml转义字符
2013-12-18 09:29 1559" " ' & ... -
字体背景颜色的高度修改
2013-12-11 10:31 4147当使用android:lineSpacingExtra= ... -
屏保的实现
2013-12-07 10:27 2729最近需要做一个屏保,开始以为很简单,因为系统本身就带了屏保功 ... -
PreferenceActivity下嵌套PreferenceScreen在其它布局中
2013-11-21 16:32 9102今天在修改系统代码的时候,系统代码用了PreferenceA ...
相关推荐
比较好用的电子教鞭,可以不锁定屏幕情况下画图。
cartopy画图-降雨,cartopy画图-降雨
Unity 画图-画板-画笔
如何使用pandas解析数据并存储,再利用matplotlib画图------data1.csv
海龟画图-凯蒂猫-python代码
vs平台,基于qt的画图--graphics练习 使用的c++ 实现椭圆,颜色改变,添加图形,view里面右键菜单,添加删除item
在线画图-流线图形在线设计.zip在线画图-流线图形在线设计.zip在线画图-流线图形在线设计.zip
11、学习画图--电脑基础知识.docx
海龟画图-机器猫-python代码
项目管理-画图-画流程图-画甘特图
画图工具源码.zip
python画图-21-列表删除数据.ev4.rar
画图程序源码.zip
python画图-22-列表修改数据.ev4.rar
python mat 画图-樱花树
Matlab画图-MatLab2画图.doc Matlab画图教程,很实用
【国家自然科学基金】-画图-基金支持热词逐年推荐-【万方软件创新助手】-20140801.pdf
python画图-20-列表增加数据之insert.ev4.rar
dino用opengl描点画图-恐龙 经典题目 不用多说了吧。
地震数据剖面画图-Matlab程序