安卓学习3-----include&drawable&ScrollView
来源:互联网 发布:小甲鱼python课件ppt 编辑:程序博客网 时间:2024/05/22 06:54
.xml
include标签即加载别处的xml,便于代码重复利用
DRAWABLE
从资源图像文件中创建
一个比较简单的方法是添加一个图片到你的程序中,然后通过资源文件引用这个文件,支持的文件类型有PNG(首选的) JPG(可接受的)GIF(不建议),显然这种对于显示应用程序的图标跟来说是首选的方法,也可以用来显示LOGO,其余的图片可以用在例如游戏中。
把一个图片资源,添加你的文件到你工程中res/drawable/目录中去,从这里,你就可以引用它到你的代码或你的XML布局中,也就是说,引用它也可以用资源编号,比如你选择一个文件只要去掉后缀就可以了(例如:my_image.png 引用它是就是my_image)。
注意:SDK指出,为了缩小图片的存储空间,在Build的时候又可能对图片进行压缩,如果不想被压缩,可以将图片放在res/raw/目录中。
SDK给出的例子:
LinearLayout mLinearLayout;protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create a LinearLayout in which to add the ImageView mLinearLayout = new LinearLayout(this); // Instantiate an ImageView and define its properties ImageView i = new ImageView(this); i.setImageResource(R.drawable.my_image); i.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); // Add the ImageView to the layout and set the layout as the content view mLinearLayout.addView(i); setContentView(mLinearLayout);}
获取Drawable对象:
Resources res = mContext.getResources();Drawable myImage = res.getDrawable(R.drawable.my_image);
注意:保持每个资源类型的一至,可以保证你项目状态的一致性,就不用担心有许多不同类型的对象来实例化它。例如:如果使用相同的图像资源来实例化两个Drawable对象。然后修改一个Drawables的属性(例如alpha),然后不幸得是这个效果也会出现在另一个对象上去。所以当处理同一个资源的多个实例对象时,不是直接转换为Drawable,而是应该执行tween animation
如何添加资源到ImageView:
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:tint="#55ff0000" android:src="@drawable/my_image"/>
从XML文件中创建
到如今,你应该比较熟悉按Android的原则去开发一个用户接口,因此,你也应该理解了定义一个XML文件对于对象的作用与灵活的重要性。这个理念无数次用于Drawables.
如果你想创建一个Drawable对象,而这个对象并不依赖于变量或用户的交换,把它定义到XML中去应该是一个不错的方法。即使你期望在你的应用程序中改变其属性来增加用户体验。你应该考虑把对象放入XML中,因为你可以随时修改其属性。
当你在你的XML中定义了一个Drawable,保存这个XML文件到你工程目录下res/drawable目录中,然后通过调用Resource.getDrawable()来检索并实例化,传递给它XML文件中的资源ID号。任何Drawable的子类都支持inflate这个方法,这个方法会通过XML来实例化你的程序。任何Drawable都支持XML的扩展来利用特殊的XML属性来帮助定义对象的属性,可以查看任何Drawable子类文档来看如何定义XML文件。
如下定义了一个TransitionDrawable:An extension of LayerDrawables that is intended to cross-fade between the first and second layer. It can be defined in an XML file with the<transition>
element. Each Drawable in the transition is defined in a nested<item>
. 有关TransitionDrawable的详细信息查看http://androidappdocs.appspot.com/reference/android/graphics/drawable/TransitionDrawable.html。
将其定义在res/drawable/expand_collapse.xml:
<?xml version="1.0" encoding="utf-8"?><transition xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/pic1"/> <item android:drawable="@drawable/pic2"/></transition>
下面实例化并处理:
public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Resources res=getResources(); TransitionDrawable trans=(TransitionDrawable )res.getDrawable(R.drawable.expand_collapse); ImageView image = (ImageView)findViewById(R.id.ImageView01); image.setImageDrawable(trans); trans.startTransition(3000); }}
直接引用图片:
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/a_0"/>
ScrollView滚动视图是指当拥有很多内容,屏幕显示不完时,需要通过滚动跳来显示的视图。ScrollView只支持垂直滚动。
- 安卓学习3-----include&drawable&ScrollView
- 安卓学习笔记之Drawable
- 安卓Drawable改变大小
- 安卓drawable和mipmap
- 安卓drawable标签详解
- 安卓学习笔记--- Android加载Gif动画android-gif-drawable的使用
- 安卓 Drawable源码分析简述
- 安卓学习笔记---实现两个ListView或者GridView在Scrollview中同时滑动,实现ScrollView滑动到顶部
- 安卓学习之路_20140424_AudioManager、MediaPlayer的综合运用、ScrollView、Gallery
- 【安卓学习之常见问题】 ScrollView与其他组件的冲突问题
- 安卓学习之解决ScrollView嵌套自定义上拉加载的Listview事件冲突
- 安卓学习笔记---ScrollView中嵌套recycleView 出现的不显示,显示不全,终极解决方案
- 安卓 坑爹的 include 标签
- 安卓布局优化Include merge ViewStub
- 安卓-通过include导入标题栏布局
- 安卓xml文件中<include>用法
- 安卓ScrollView实现自动滚屏
- 安卓自定义Scrollview,实现卷帘效果
- 开灯问题
- 浅析ptmx代码级open如何运作ptyp,ttyp,pts伪终端(转载)
- 2013.08.08(2)
- DCP-7060D一体机加粉清零
- 第3.3.1节 处理手势操作
- 安卓学习3-----include&drawable&ScrollView
- Android的Message机制(简单小结)
- hdu 题目1236 排名
- 通过金矿模型介绍动态规划
- 守护进程(daemon)
- 利用 NSSortDescriptor 对数组进行排序
- STL 简介
- 计数排序(线性)
- Objective-C——消息、Category和Protocol