按代码更改按下的图像资源

时间:2012-01-03 15:22:15

标签: android android-layout

是否可以对图像进行一些图像处理以模拟按下状态? (减少APK的大小)

1 个答案:

答案 0 :(得分:1)

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:drawable="@drawable/button_pressed" /> <!-- pressed -->
     <item android:state_focused="true"
           android:drawable="@drawable/button_focused" /> <!-- focused -->
     <item android:drawable="@drawable/button_normal" /> <!-- default -->
 </selector>

然后每个按钮也可以用XML定义:

button_normal:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#DD63594A"
        android:endColor="#809C7D5A"
        android:angle="90"/>
    <stroke android:width="2dp" android:color="#FF524539" />
        <!-- android:dashWidth="2dp" android:dashGap="1dp" /> -->
    <padding android:left="4dp"
        android:top="4dp"
        android:right="4dp"
        android:bottom="4dp"/>
    <corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp" 
        android:topLeftRadius="7dp" android:topRightRadius="7dp"/>
</shape> 

button_pressed:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#80A57D08"
        android:endColor="#DDE75D31"
        android:angle="90"/>
    <stroke android:width="2dp" android:color="#FF524539" />
       <!-- android:dashWidth="2dp" android:dashGap="1dp" /> -->
    <padding android:left="4dp"
        android:top="4dp"
        android:right="4dp"
        android:bottom="4dp"/>
    <corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp" 
        android:topLeftRadius="7dp" android:topRightRadius="7dp"/> 
</shape> 

button_focused:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#80E75D31"
        android:endColor="#DDE75D31"
        android:angle="90"/>
    <stroke android:width="2dp" android:color="#FF524539" />
        <!-- android:dashWidth="2dp" android:dashGap="1dp" /> --> 
    <padding android:left="4dp"
        android:top="4dp"
        android:right="4dp"
        android:bottom="4dp"/>
    <corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp" 
        android:topLeftRadius="7dp" android:topRightRadius="7dp"/> 
</shape> 

就动画而言,它实际上取决于你想做什么以及结束:

您可以制作自己的(逐帧),例如:http://www.twintechs.com/2008/06/frame-by-frame-xml-animation-with-google-android/&amp; http://code.google.com/p/android-animation-example/

或使用AnimatedViews:http://mylifewithandroid.blogspot.com/2008/04/animated-views.html

希望有所帮助。这将节省空间; - )

仅供参考:如果您正在寻找压缩,请查看http://trimage.org/

相关问题