`
hao3100590
  • 浏览: 130830 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Android下基于XML的 Graphics(转载)

阅读更多

Android下基于XML的 Graphics

以前作图,一般有两种方式,首先是UI把图形设计好,我们直接贴,对于那些简单的图形,如矩形、扇形这样的图
形,一般的系统的API会提供这样的接口,但是在Android下,有第三种画图方式,介于二者之间,结合二者的长处,如
下的代码: 
Java 代码 
    <item android:id="@android:id/secondaryProgress"> 
        <clip> 
          
            <shape> 
                <corners android:radius="5dip" /> 
                <gradient 
                        android:startColor="#0055ff88" 
                        android:centerColor="#0055ff00" 
                        android:centerY="0.75" 
                        android:endColor="#00320077" 
                        android:angle="270" 
                /> 
            </shape> 
        </clip> 
    </item> 
这是一个Progress的style里面的代码,描述的是进度条的为达到的图形,原本以为这是一个图片,后来仔细的跟踪代码,
发现居然是 xml,像这种shape corners gradient等等这还是第一次碰到。shape 表示是一个图形,corners表示是有半径
为5像素的圆角,然后,gradient表示一个渐变。这样作图简单明了,并且可以做出要求很好的图形,并且节省资源

Java 代码 
<shape xmlns:android="
http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
   <gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF" 
            android:angle="270"/> 
   <padding android:left="50dp" android:top="20dp" 
            android:right="7dp" android:bottom="7dp" /> 
   <corners android:radius="8dp" /> 
</shape>
gradient 产生颜色渐变 android:angle 从哪个角度开始变 貌似只有90的整数倍可以 
android:shape="rectangle" 默认的也是长方形

Java 代码 
<shape xmlns:android="
http://schemas.android.com/apk/res/android" android:shape="oval"> 
   <solid android:color="#ff4100ff"/> 
   <stroke android:width="2dp" android:color="#ee31ff5e" 
            android:dashWidth="3dp" android:dashGap="2dp" /> 
   <padding android:left="7dp" android:top="7dp" 
            android:right="7dp" android:bottom="7dp" /> 
   <corners android:radius="6dp" /> 
</shape> 
#ff4100ff蓝色#ff4100ff绿色 
<solid android:color="#ff4100ff"/>实心的 填充里面 
<stroke 描边 采用那样的方式将外形轮廓线画出来 
android:dashWidth="3dp" android:dashGap="2dp" 默认值为0 
android:width="2dp" android:color="#FF00ff00"笔的粗细, 
android:dashWidth="5dp" android:dashGap="5dp" 实现- - -这样的效果,dashWidth指的是一条小横线的宽度 
dashGap 指的是 小横线与小横线的间距。 width="2dp" 不能太宽 


shape等特殊xml

1.用 shape 作为背景 
<shape xmlns:android="
http://schemas.android.com/apk/res/android"> 
   <solid android:color="#f0600000"/> 
   <stroke android:width="3dp" color="#ffff8080"/> 
   <corners android:radius="3dp" /> 
   <padding android:left="10dp" android:top="10dp" 
    android:right="10dp" android:bottom="10dp" /> 
</shape> 
一定要注意solid android:color="#f0600000" 是背景色 要用8位 最好不要完全透明不然没有效果啊 这句话本来就不
是背景色 的意思

2.类似多选的效果: 
(1) listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

listView.setItemsCanFocus(false); 
(2) define list item 
CheckedTextView xmlns:android="
http://schemas.android.com/apk/res/android
   android:orientation="vertical" 
   android:layout_width="fill_parent" 
   android:layout_height="?android:attr/listPreferredItemHeight" 
   android:textAppearance="?android:attr/textAppearanceLarge" 
   android:gravity="center_vertical" 
   android:paddingLeft="6dip" 
   android:paddingRight="6dip" 
   android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
   android:background="@drawable/txt_view_bg"/> 
(3) define drawable txt_view_bg.xml <item android:drawable="@drawable/selected" android:state_checked="true" /> <item android:drawable="@drawable/not_selected" />

3. 
<LinearLayout    android:layout_width ="100dp"    android:layout_height="wrap_content" /> 
LinearLayour ll = new LinearLayout(this);parentView.addView(ll, new LinearLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT));

4. 当设置   TextView setEnabled(false)时 背景颜色你如果用#ffff之类的话可能不会显示 你最好使用 android:textColor这个属性而不是使用color。 
<TextView    android:text="whatever text you want"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:textColor="@color/example" />

res/color/example.xml):

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_enabled="false" android:color="@color/disabled_color" />    <item android:color="@color/normal_color"/></selector>

http://developer.android.com/intl/zh-CN/reference/android/content/res/ColorStateList.html

5. 
http://android.amberfog.com/?p=9 
<layer-list xmlns:android="
http://schemas.android.com/apk/res/android"> 
   <item> 
      <shape> 
         <solid android:color="#FFF8F8F8" /> 
      </shape> 
   </item> 
   <item android:top="23px"> 
      <shape> 
         <solid android:color="#FFE7E7E8" /> 
      </shape> 
   </item> 
</layer-list> 
You can simple combine several drawables into one using <layer-list> tag. 
note: Unfortenately you cannot resize drawables in layer-list. You can only move it.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">   <item android:drawable="@drawable/shape_below"/>   <item android:top="10px" android:right="10px" android:drawable="@drawable/shape_cover"/></layer-list>


include 
You can put similar layout elements into separate XML and use <include> tag to use it. 
<RelativeLayout xmlns:android="
http://schemas.android.com/apk/res/android
    android:layout_width="fill_parent" 
    android:layout_height="64dip" 
    android:gravity="center_vertical" 
    android:ignoreGravity="@+id/icon">

    <include layout="@layout/track_list_item_common" />;

</RelativeLayout>

track_list_item_common.xml 
<merge xmlns:android="
http://schemas.android.com/apk/res/android">

    <ImageView android:id="@+id/icon" 
        android:layout_alignParentLeft="true" 
        android:layout_centerVertical="true" 
        android:layout_marginLeft="4dip" 
        android:layout_width="60px" 
        android:layout_height="60px"/> 
... 
</merge>

分享到:
评论

相关推荐

    xmlgraphics-commons-2.1-API文档-中文版.zip

    赠送jar包:xmlgraphics-commons-2.1.jar; 赠送原API文档:xmlgraphics-commons-2.1-javadoc.jar; 赠送源代码:xmlgraphics-commons-2.1-sources.jar; 赠送Maven依赖信息文件:xmlgraphics-commons-2.1.pom; ...

    xmlgraphics-commons-2.1-API文档-中英对照版.zip

    赠送jar包:xmlgraphics-commons-2.1.jar; 赠送原API文档:xmlgraphics-commons-2.1-javadoc.jar; 赠送源代码:xmlgraphics-commons-2.1-sources.jar; 赠送Maven依赖信息文件:xmlgraphics-commons-2.1.pom; ...

    xmlgraphics1.4.jar

    2. **PDF生成**:XMLGraphics库可以用于生成PDF文档,包括插入SVG图像或直接创建基于XML的PDF内容。 3. **图像处理**:通过XMLGraphics Commons,开发者可以处理图像资源,如缩放、裁剪或转换图像格式。 4. **文本...

    xmlgraphics-commons-2.6.jar

    xmlgraphics-commons-2.6.jar

    android下解析xml文件的demo

    本教程将详细介绍如何在Android环境下解析XML文件,以"ReadXMLDemo"为例,展示具体步骤和关键知识点。 首先,XML(eXtensible Markup Language)是一种自定义标记语言,它的结构清晰,易于读写,适合于数据交换和...

    android String.xml Excel 相互转换工具

    `String.xml`是Android资源文件夹下的一个XML文件,用于存放应用中的所有字符串资源。每个字符串都有一个唯一的ID,开发者可以通过这个ID在代码中引用字符串,而不用直接硬编码文本。例如: ```xml ...

    xmlgraphics-commons-1.5.jar

    xmlgraphics-commons-1.5.jar

    xmlgraphics-commons:Apache XML Graphics Commons的镜像

    这个库提供了许多通用的功能,这些功能在创建和处理基于XML的图形时非常有用,如SVG(Scalable Vector Graphics)和PDF(Portable Document Format)文档。XMLGraphics Commons的镜像是该项目的某个特定版本或开发...

    android读写xml文件

    android中读写xml文件简单demo,可以通过此代码来简单的读取xml配置文件

    Android中在xml中静态添加Fragment

    在Android应用开发中,Fragment是Android SDK提供的一种组件,它允许开发者在单个Activity中实现多个可交互的屏幕区域。Fragment的设计使得应用可以更好地适应不同的屏幕尺寸和配置,如手机和平板。本教程将深入讲解...

    Android-Android国际化文档整理xml和excel互转

    4. 按照Android的目录结构,将转换后的XML文件放入相应的`values-xx`目录下。 五、注意事项 在进行转换时,确保保留XML文件中的注释和格式,因为它们可能包含了特定的指示信息。同时,Excel文件中的列名应与XML...

    Android dimens.xml 不同分辨率生成

    Android 工程里面values文件夹有dimens.xml文件,为了适配不同的分辨率,例如values-sw400dp,values-sw600dp,values-sw720dp,如果自己去生成这几个文件夹,需要根据values里面的dimens.xml进行计算,然后修改,这个...

    android颜色xml配置

    Android的颜色资源通常存储在`res/values`目录下的`colors.xml`文件中。这是一个XML文件,其中定义了一系列的颜色资源,如: ```xml &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;color name="primary_color"&gt;#3...

    Android xml布局文件生成工具

    在Android应用开发中,XML布局文件是构建用户界面(UI)的主要方式,它允许开发者以声明式编程的方式定义UI元素的结构和样式。"Android xml布局文件生成工具"是为了解决手动编写XML布局文件繁琐和耗时的问题而设计的...

    pom.xml android maven 工程

    pom.xml android maven 工程 如何拿Maven 构建 一个 Android 项目的pom配置

    六,Android Xml解析

    4. **资源文件**:例如字符串资源、颜色资源、尺寸资源等,都在values目录下的XML文件中定义,Android系统在运行时会自动解析。 5. **网络数据交换**:XML常用于服务器与客户端之间的数据交换,通过HTTP或FTP等方式...

    XML同学录毕业论文(基于XML的在线系统分析与设计)

    本菜鸟的毕业设计源代码,一个简单的基于.NET平台和XML数据源的同学录,本系统对XML增、删、改、查等操作都有,并有详细注释,适合和我一样的菜鸟们学习。^_^ 超级管理员登陆账号和密码都是txl !

    将AndroidStudio 的 VectorDrawable xml 资源逆向转回 svg文件

    自制工具,将AndroidStudio 的 VectorDrawable xml 资源逆向转回 svg文件方便编辑。工具为带js的html,上方输入框贴入xml,下方输入框输出svg代码结果以及svg预览 注意目前仍未支持渐变填充

    基于XML的SVG应用指南

    缩放矢量图形(Scalable Vector Graphics,SVG)是基于可扩展标记语言(XML),用于描述二维矢量图形的一种图形格式。SVG由W3C制定,是一个开放标准

    Android XML attribute

    ### Android XML Attribute详解 在Android开发中,XML文件扮演着非常重要的角色,特别是在定义界面布局、样式、动画等方面。为了帮助开发者更好地理解Android中的各种XML属性及其用途,本文将详细介绍部分核心XML...

Global site tag (gtag.js) - Google Analytics