一.怎么进行缩放填充图像到ImageButton
android:scaleType="fitXY“
android:scaleType 设置图像的填充方式
fitXY 把图片不按比例扩大/缩小到View的大小显示
说一下要注意的:
这么做的话,如果控件的长宽比和图像的长宽比不同的话就与比较严重的失真。所以要多注意一下.尤其是多分辨率适配的时候。多做真机调试
使用这个方法填充的时候是必须配合android:src来使用的。对于android:background是无效的.
android:src="@drawable/miss" 改为 android:background="@drawable/miss"
首先background是填充背景的属性。指定后会根据ImageView组件给定的长宽进行拉伸。
而边框的存在就是因为背景的存在。所以能替代第一种解决方案
(src是图像内容(前景),background是背景,可以同时使用。)
二、背景渐变
在drawable新建一XML
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient //渐变
android:startColor="#ff9bd0f3"
android:endColor="#ff2558af"
android:angle="225" /> //渐变角度,0度从左上开始逆时针 ,225度为右上角开始
<corners android:radius="3dp" /> //角度
<stroke android:width="5px" android:color="#000000" /> 边框
</shape>
三、点击效果
1.点击动画:
在drawable新建一XML
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@color/switch_thumb_normal_material_dark" /> //点击效果
<item
android:state_enabled="true"
android:drawable="@drawable/background_deepblue" /> //静止效果
</selector>
Note: 默认状态应该放到列表的最后,要不每次开始就固定为默认状态而不会执行其他事件状态
2.点击后图片不回弹(此处用代码实现)
efaxHeadButton.setBackgroundResource(R.drawable.efax_tab_inbox); //set background image efaxHeadButton.setOnClickListener(new View.OnClickListener() { boolean isIconChange = false; @Override public void onClick(View v) { if (isIconChange) { //when isIconChange is true,default image efaxHeadButton.setBackgroundResource(R.drawable.efax_tab_inbox); isIconChange = false; } else { //when isIconChange is false,clicked image efaxHeadButton.setBackgroundResource(R.drawable.efax_tab_inbox_p); isIconChange = true; } } }); }
3.设置右上角消息提醒
使用嵌套
<RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <ImageButton android:id="@+id/myfaxpgage_head_button1" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" android:background="#ffebebeb" /> <TextView android:id="@+id/myfaxpgage_head_message1" android:layout_width="30dp" android:layout_height="30dp" android:text="2" android:textSize="28sp" android:gravity="center" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:background="@drawable/textview" /> </RelativeLayout>
testview圆圈背景
新建drawable/textview.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <!-- android API里有如下方法,但经测试只有 rectangle有用,其他均在调用处空白 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape=["rectangle" | "oval" | "line" | "ring"] > --> <solid android:color="#ffff3f29" /> <corners android:radius="100dp" /> </shape>