安卓学习-资源访问

安卓学习-资源访问

1.静态原生的资源,都保存在assets里,如db,mp3等

2.其他的一般都通过R.访问

 

目录 存放资源 说明
/res/animator 属性动画的XML  颜色切换动画;3D旋转动画
/res/anim 补件动画的XML  只能支持简单的缩放、平移、旋转、透明度基本的动画,且有一定的局限性
 /res/color  颜色  
 /res/drawable

 存放各种位图文件和Drawable的XML文件

如:

*.png,*.9.png,*.jpg,*.gif

BitmapDrawable

NinePatchDrawable

StateListDrawable

ShapeDrawable

AnimationDrawable

Drawable各类其他子对象

 NinePatchDrawable(*.9.png)

StateListDrawable根据不同状态,变换背景色

ShapeDrawable 画图用的

AnimationDrawable 动画

 /res/layout  存放界面布局文件  
 /res/menu 存放菜单   
 /res/raw

 存放一些原生资源,如声音、视频等

 

InputStream in=getResources().openRawResource(R.raw.aaa);

现在已经不建议放这里了,直接放assets

然后通过InputStream in=getResources().getAssets().open("aaa.txt");

 这些数据只能读取,不能写入。更重要的是该目录下的文件大小不能超过1M。

 

 /res/values

 自定各种资源文件

如:

arrays.xml

strings.xml

colors.xml

demens.xml

styles.xml

 
 /res/xml  原生的xml文件可以存放在这里  pull方式
try {
            XmlResourceParser xml = getResources().getXml(R.xml.bbb);
            xml.next();
            int eventType = xml.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                // 如过到达标记的节点则取出内容
                if (eventType == XmlPullParser.TEXT) {
                    Log.v("weijj", EncodingUtils.getString(xml.getText().getBytes(), "utf-8"));
                }
                xml.next();
                eventType = xml.getEventType();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
View Code

 dom方式,还是比较喜欢这个

xml文件

<?xml version="1.0" encoding="UTF-8"?>
<China>
 <province id="21" name="浙江">
    <city id="2101" name="杭州">
      <county id="210101" name="杭州" weatherCode="101210101"/>
      <county id="210102" name="萧山" weatherCode="101210102"/>
      <county id="210103" name="桐庐" weatherCode="101210103"/>
      <county id="210104" name="淳安" weatherCode="101210104"/>
      <county id="210105" name="建德" weatherCode="101210105"/>
      <county id="210106" name="余杭" weatherCode="101210106"/>
      <county id="210107" name="临安" weatherCode="101210107"/>
      <county id="210108" name="富阳" weatherCode="101210108"/>
    </city>
    <city id="2102" name="湖州">
      <county id="210201" name="湖州" weatherCode="101210201"/>
      <county id="210202" name="长兴" weatherCode="101210202"/>
      <county id="210203" name="安吉" weatherCode="101210203"/>
      <county id="210204" name="德清" weatherCode="101210204"/>
    </city>
    <city id="2103" name="嘉兴">
      <county id="210301" name="嘉兴" weatherCode="101210301"/>
      <county id="210302" name="嘉善" weatherCode="101210302"/>
      <county id="210303" name="海宁" weatherCode="101210303"/>
      <county id="210304" name="桐乡" weatherCode="101210304"/>
      <county id="210305" name="平湖" weatherCode="101210305"/>
      <county id="210306" name="海盐" weatherCode="101210306"/>
    </city>
    <city id="2104" name="宁波">
      <county id="210401" name="宁波" weatherCode="101210401"/>
      <county id="210402" name="慈溪" weatherCode="101210403"/>
      <county id="210403" name="余姚" weatherCode="101210404"/>
      <county id="210404" name="奉化" weatherCode="101210405"/>
      <county id="210405" name="象山" weatherCode="101210406"/>
      <county id="210406" name="宁海" weatherCode="101210408"/>
      <county id="210407" name="北仑" weatherCode="101210410"/>
      <county id="210408" name="鄞州" weatherCode="101210411"/>
      <county id="210409" name="镇海" weatherCode="101210412"/>
    </city>
    <city id="2105" name="绍兴">
      <county id="210501" name="绍兴" weatherCode="101210501"/>
      <county id="210502" name="诸暨" weatherCode="101210502"/>
      <county id="210503" name="上虞" weatherCode="101210503"/>
      <county id="210504" name="新昌" weatherCode="101210504"/>
      <county id="210505" name="嵊州" weatherCode="101210505"/>
    </city>
    <city id="2106" name="台州">
      <county id="210601" name="台州" weatherCode="101210601"/>
      <county id="210602" name="玉环" weatherCode="101210603"/>
      <county id="210603" name="三门" weatherCode="101210604"/>
      <county id="210604" name="天台" weatherCode="101210605"/>
      <county id="210605" name="仙居" weatherCode="101210606"/>
      <county id="210606" name="温岭" weatherCode="101210607"/>
      <county id="210607" name="洪家" weatherCode="101210609"/>
      <county id="210608" name="临海" weatherCode="101210610"/>
      <county id="210609" name="椒江" weatherCode="101210611"/>
      <county id="210610" name="黄岩" weatherCode="101210612"/>
      <county id="210611" name="路桥" weatherCode="101210613"/>
    </city>
    <city id="2107" name="温州">
      <county id="210701" name="温州" weatherCode="101210701"/>
      <county id="210702" name="泰顺" weatherCode="101210702"/>
      <county id="210703" name="文成" weatherCode="101210703"/>
      <county id="210704" name="平阳" weatherCode="101210704"/>
      <county id="210705" name="瑞安" weatherCode="101210705"/>
      <county id="210706" name="洞头" weatherCode="101210706"/>
      <county id="210707" name="乐清" weatherCode="101210707"/>
      <county id="210708" name="永嘉" weatherCode="101210708"/>
      <county id="210709" name="苍南" weatherCode="101210709"/>
    </city>
    <city id="2108" name="丽水">
      <county id="210801" name="丽水" weatherCode="101210801"/>
      <county id="210802" name="遂昌" weatherCode="101210802"/>
      <county id="210803" name="龙泉" weatherCode="101210803"/>
      <county id="210804" name="缙云" weatherCode="101210804"/>
      <county id="210805" name="青田" weatherCode="101210805"/>
      <county id="210806" name="云和" weatherCode="101210806"/>
      <county id="210807" name="庆元" weatherCode="101210807"/>
      <county id="210808" name="松阳" weatherCode="101210808"/>
      <county id="210809" name="景宁" weatherCode="101210809"/>
    </city>
    <city id="2109" name="金华">
      <county id="210901" name="金华" weatherCode="101210901"/>
      <county id="210902" name="浦江" weatherCode="101210902"/>
      <county id="210903" name="兰溪" weatherCode="101210903"/>
      <county id="210904" name="义乌" weatherCode="101210904"/>
      <county id="210905" name="东阳" weatherCode="101210905"/>
      <county id="210906" name="武义" weatherCode="101210906"/>
      <county id="210907" name="永康" weatherCode="101210907"/>
      <county id="210908" name="磐安" weatherCode="101210908"/>
    </city>
    <city id="2110" name="衢州">
      <county id="211001" name="衢州" weatherCode="101211001"/>
      <county id="211002" name="常山" weatherCode="101211002"/>
      <county id="211003" name="开化" weatherCode="101211003"/>
      <county id="211004" name="龙游" weatherCode="101211004"/>
      <county id="211005" name="江山" weatherCode="101211005"/>
      <county id="211006" name="衢江" weatherCode="101211006"/>
    </city>
    <city id="2111" name="舟山">
      <county id="211101" name="舟山" weatherCode="101211101"/>
      <county id="211102" name="嵊泗" weatherCode="101211102"/>
      <county id="211103" name="岱山" weatherCode="101211104"/>
      <county id="211104" name="普陀" weatherCode="101211105"/>
      <county id="211105" name="定海" weatherCode="101211106"/>
    </city>
  </province>
  </China>
  
View Code

解析方法

 String str="";
                        InputStream is = getAssets().open("tq.xml");
                        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  //取得DocumentBuilderFactory实例  
                        DocumentBuilder builder = factory.newDocumentBuilder(); //从factory获取DocumentBuilder实例  
                        Document doc = builder.parse(is);   //解析输入流 得到Document实例      
                        Element rootElement = doc.getDocumentElement();  

                        NodeList items = rootElement.getElementsByTagName("province");  
                        Element item=(Element)items.item(0);
                        //获得省份
                        str=str+item.getAttribute("name")+"\n";
                        
                        //城市
                        items=item.getElementsByTagName("city"); 
                        for(int i=0;i<items.getLength();i++){
                            Element itemCity=(Element)items.item(i);
                            str=str+itemCity.getAttribute("name")+":";
                            
                            NodeList itemsXq=itemCity.getElementsByTagName("county"); 
                            for(int j=0;j<itemsXq.getLength();j++){
                                Element itemXq=(Element)itemsXq.item(j);
                                str=str+itemXq.getAttribute("name")+",";
                            }
                            str=str+"\n\n";
                        }
    
                        editText1.setText(str);
                        
View Code

 

 

Drawable资源使用

1.BitmapDrawable

        Drawable drawable=getResources().getDrawable(R.drawable.ic_launcher);
        ImageView imageView1=(ImageView)findViewById(R.id.imageView1);
        imageView1.setBackground(drawable);

2.StateListDrawable

 支持的状态

属性值 含义
android:state_active 激活状态
android:state_checkable 可勾选状态
android:state_checked 已勾选
android:state_enabled 可用
android:state_first 开始状态
android:state_focused 得到焦点状态
android:state_last 结束状态
android:state_middle 中间状态
android:state_pressed 被安装状态
android:state_selected 被选中状态
android:state_window_focused 窗口是否已获得焦点

例子:录入汉字是字体变色

edt1.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_focused="true" android:color="#EE2C2C" />
    <item android:state_focused="false" android:color="#0A0A0A" />
</selector>
View Code

 

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:ems="10"
        android:textColor="@drawable/edt1" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="26dp"
        android:ems="10"
        android:textColor="@drawable/edt1" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText2"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="30dp"
        android:ems="10"
        android:textColor="@drawable/edt1" />

</RelativeLayout>
View Code

 

3.LayerDrawable

包含drawable数组,根据索引,从大到小排列

例子1:层叠图片

layer1.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
   <item>
       <bitmap android:src="@drawable/a11" android:gravity="left"/>
   </item>
     <item android:top="40dp" android:left="40dp">
       <bitmap android:src="@drawable/a22" android:gravity="left" />
   </item>
     <item android:top="80dp" android:left="80dp">
       <bitmap android:src="@drawable/a33" android:gravity="left"/>
   </item>
</layer-list>
View Code

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="24dp"
        android:src="@drawable/layer1" />

</RelativeLayout>
View Code

 例子2:网上找的,自定义样式

bg_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<!--自定义SeekBar的背景定义为:android:progressDrawable="@drawable/bg_bar"-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 背景图 -->
    <item android:id="@+android:id/background" android:drawable="@drawable/bar_up" />
    <!-- 第二进度图 -->
    <item android:id="@+android:id/SecondaryProgress" android:drawable="@drawable/bar_up" />
    <!-- 进度度 -->
    <item android:id="@+android:id/progress" android:drawable="@drawable/bar_dn" />
</layer-list>
View Code

thumb_bar.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 自定义SeekBar的滑块定义为:android:thumb="@drawable/thumb_bar" -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 按下状态 -->
    <item android:state_pressed="true"
        android:drawable="@drawable/thumb_dn" />

    <!-- 焦点状态 -->
    <item android:state_focused="true"
        android:drawable="@drawable/thumb_up" />
    
    <!-- 默认状态 -->
    <item android:drawable="@drawable/thumb_up" />  
    
</selector> 
View Code

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView
        android:id="@+id/tv_def"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="系统自带" />
    
    <!-- 
        max=100,代表它的取值范围是0-100,共101个值;
        progress=10,代表默认值是10  
    -->

    <SeekBar
        android:id="@+id/seekbar_def"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="10" />
    
    <TextView
        android:id="@+id/tv_self"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定义的" />
    
    <!-- 
        max=100,代表它的取值范围是0-100,共101个值;
        progress=20,代表默认值是20
        progressDrawable,表示SeekBar的背景图片
        thumbe,表示SeekBar的滑块图片  
    -->

    <SeekBar
        android:id="@+id/seekbar_self"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="20"
        android:progressDrawable="@drawable/bg_bar"
        android:thumb="@drawable/thumb_bar" />
    
</LinearLayout>
View Code

 用到的一些图片资源

bar_dn.png

bar_up.png

thumb_dn.png

thumb_up.png

 

3.ShapeDrawable

用于定义一个几何图形

android:sharp=["rectangle"|"oval"|"line"|"ring"]

rectangle:矩形

oval:椭圆形

line:线条

ring:环形

例子1

shap1.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- 填充色 橘黄色 -->
    <solid android:color="#FF7F24"/>
    <!-- 定义四个角的弧度 -->
    <corners android:radius="30dp"  />
    <!--定义描边的宽度和描边的颜色值 -->
     <stroke android:width="2dip"  android:color="#7D26CD"/> 
</shape>
View Code

例子2

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
   <!-- 渐变 -->
   <gradient android:startColor="#FFF5EE" android:endColor="#FF1493" />
</shape>
View Code

例子3

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
   <!-- 渐变 -->
   <gradient android:startColor="#FFF5EE" android:endColor="#FF1493" android:type="sweep" />
</shape>
View Code

 

4.ClipDrawable 从其他图片上,截图一部分

可通过调用setLevel来显示截取图片的区域大小,10000表示显示整张图片

例子

一个从左到右,从上到下

 

clip1.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- android:drawable 图片源 
    android:clipOrientation 方向
    android:gravity 对其方式
-->
<clip xmlns:android="http://schemas.android.com/apk/res/android" 
    android:drawable="@drawable/pad"
    android:clipOrientation="vertical"
    android:gravity="top">
 </clip>
View Code

clip2.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- android:drawable 图片源 
    android:clipOrientation 方向
    android:gravity 对其方式
-->
<clip xmlns:android="http://schemas.android.com/apk/res/android" 
    android:drawable="@drawable/pad"
    android:clipOrientation="horizontal"
    android:gravity="left">
 </clip>
View Code

MainActivity.java

public class MainActivity extends Activity {

    ClipDrawable clip1;
    ClipDrawable clip2;
    ImageView imageView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        clip2 = (ClipDrawable) getResources().getDrawable(R.drawable.clip2);

        imageView1 = (ImageView) findViewById(R.id.imageView1);

        Button btn1 = (Button) findViewById(R.id.button1);
        Button btn2 = (Button) findViewById(R.id.button2);

        btn1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                clip1 = (ClipDrawable) getResources().getDrawable(
                        R.drawable.clip1);
                imageView1.setBackground(clip1);
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {

                    @Override
                    public void run() {
                        Message msg = new Message();
                        msg.what = 1;
                        handler.sendMessage(msg);
                    }
                }, 0, 1000);

            }
        });
        
        
        btn2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                clip2 = (ClipDrawable) getResources().getDrawable(
                        R.drawable.clip2);
                imageView1.setBackground(clip2);
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {

                    @Override
                    public void run() {
                        Message msg = new Message();
                        msg.what = 1;
                        handler.sendMessage(msg);
                    }
                }, 0, 1000);

            }
        });
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                if (clip1 != null) {
                    clip1.setLevel(clip1.getLevel() + 1000);
                }
                if (clip2 != null) {
                    clip2.setLevel(clip2.getLevel() + 1000);
                }
            }
        }

    };
}
View Code

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="从上到下" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="从左到右" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1"
        android:src="@drawable/clip1" />

</RelativeLayout>
View Code

 

5.AnimataionDrawable 补间动画

透明度、缩放、位移、旋转 从开始到结束的变化

anim1.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- linear_interpolator匀速变换  -->
<!-- accelerate_interpolator加速变换  -->
<!-- decelerate_interpolator减速变换  -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:duration="5000">
    <!-- 透明度从0 到100 -->
   <alpha android:fromAlpha="0" android:toAlpha="1"  />
</set>
View Code

anim2.xml

 

<?xml version="1.0" encoding="utf-8"?>
<!-- android:fillAfter保留最后状态  -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:duration="5000"
    android:fillAfter="true">
    <!--android:pivotX="50%"  android:pivotY="50%" 从中间位置开始动画-->
   <scale android:fromXScale="0" android:toXScale="1" 
           android:fromYScale="0" android:toYScale="1"
           android:pivotX="50%" android:pivotY="50%"
            />
</set>
View Code

anim3.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- android:fillAfter保留最后状态  -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:duration="5000"
    android:fillAfter="true">
    <translate  android:fromXDelta="0"  android:toXDelta="100"
                android:fromYDelta="0"  android:toYDelta="100"/>
</set>
View Code

anim4.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- android:fillAfter保留最后状态  -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:duration="5000"
    android:fillAfter="true">
    <!-- 从0度到360度旋转 -->
    <rotate android:fromDegrees="0"  android:toDegrees="360"
            android:pivotX="50%"  android:pivotY="50%"
        />
</set>
View Code

MainActivity.java

public class MainActivity extends Activity implements OnClickListener {

    ImageView imageView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        imageView1 = (ImageView) findViewById(R.id.imageView1);

        Button btn1 = (Button) findViewById(R.id.button1);
        Button btn2 = (Button) findViewById(R.id.button2);
        Button btn3 = (Button) findViewById(R.id.button3);
        Button btn4 = (Button) findViewById(R.id.button4);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v==findViewById(R.id.button1)){
            //透明度
            Animation anim1=AnimationUtils.loadAnimation(MainActivity.this, R.drawable.anim1);
            imageView1.startAnimation(anim1);
        }else if(v==findViewById(R.id.button2)){
            //透明度
            Animation anim1=AnimationUtils.loadAnimation(MainActivity.this, R.drawable.anim2);
            imageView1.startAnimation(anim1);
        }else if(v==findViewById(R.id.button3)){
            //透明度
            Animation anim1=AnimationUtils.loadAnimation(MainActivity.this, R.drawable.anim3);
            imageView1.startAnimation(anim1);
        }else if(v==findViewById(R.id.button4)){
            //透明度
            Animation anim1=AnimationUtils.loadAnimation(MainActivity.this, R.drawable.anim4);
            imageView1.startAnimation(anim1);
        }
        
    }
}
View Code

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="透明度" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:src="@drawable/pad" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imageView1"
        android:layout_toRightOf="@+id/button1"
        android:text="缩放" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/button2"
        android:text="位移" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/button3"
        android:text="旋转" />

</RelativeLayout>
View Code

 

6.ProtertyAnimition属性动画

ainimi1.xml

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" 
    android:propertyName="backgroundColor"
    android:duration="3000"
    android:valueFrom="#FFF5EE"
    android:valueTo="#FF1493"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:valueType="intType"
    />
    
View Code

MainActivity.java

public class MainActivity extends Activity{

    ImageView imageView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout l=(LinearLayout)findViewById(R.id.linearLayout1);
        l.addView(new MyView(this));
    }

    public class MyView extends View {

        public MyView(Context context) {
            super(context);
            
            ObjectAnimator colorAnim=(ObjectAnimator)AnimatorInflater.loadAnimator(context, R.animator.anim1);
            colorAnim.setEvaluator(new ArgbEvaluator());
            colorAnim.setTarget(this);
            colorAnim.start();
        }
        
    }
    
}
View Code

 

 

样式(style)和主题(theme)

1.样式

my_style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="style1">
        <item name="android:textSize">20sp</item>
        <item name="android:textColor">#FF7F24</item>
    </style>
    
    <style name="style2" parent="style1">
        <item name="android:background">#ee6</item>
        <item name="android:padding">8dp</item>
        <item name="android:textColor">#9ACD32</item>
    </style>
    
    
</resources>
View Code

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    style="@style/style1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        style="@style/style1"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="15dp"
        style="@style/style2"
        android:ems="10" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="18dp"
        android:ems="10" />

</RelativeLayout>
View Code

 

posted on 2014-12-31 14:18  weijj  阅读(424)  评论(0编辑  收藏  举报

导航