Document directory
- Preface
- 1 ImageView add rounded border
- 2. Set the Image view to have a filter effect. The following example shows the gray effect.
Preface
This article summarizes the effects used in ImageView development.
1 ImageView add rounded border
The following is an example
Implementation Process description:
Step One: Set the Padding of ImageView to a certain width, and set android: adjustViewBounds to True.
<ImageView android:background="@drawable/img_on" android:id="@+id/imageViewt" android:adjustViewBounds="true" android:padding="2dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/icon" />
Step Two: Set the image background
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:useLevel="false"> <corners android:radius="5dp" /> <gradient android:startColor="#848484" android:centerColor="#848484" android:useLevel="false" android:type="linear" android:angle="90" android:endColor="#848484"> </gradient> </shape>
2. Set the Image view to have a filter effect. The following example shows the gray effect.
Implementation Process description:
Use the ColorFilter attribute of the image object to set the grayscale channel of ColorMatrixColorFilter and pass it to the ColorFilter attribute.
Note two points: 1. The effect of the image filter is only added to the layer, not the actual modification of the image.
2 If you apply a filter effect to a single image, the image will also be used in other places with a filter effect.
The following is the actual implementation code
public final float[] BT_SELECTED = new float[] {1,0,0,0,99,0,1,0,0,99,0,0,1,0,99,0,0,0,1,0}; public final float[] BT_NOT_SELECTED = new float[] {1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0};; public final static float[] BT_SELECTED1 = new float[] { 0.338f, 0.339f, 0.332f, 0, 0, 0.338f, 0.339f, 0.332f, 0, 0, 0.338f, 0.339f, 0.332f, 0, 0, 0, 0, 0, 1, 0 }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView ib2; ib1 = (ImageView) findViewById(R.id.imageViewt); ib2 = (ImageView) findViewById(R.id.imageView2); ib1.setOnTouchListener(new ImageView.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { ib1.setImageResource(R.drawable.icon1); ib1.getDrawable().setColorFilter( new ColorMatrixColorFilter(BT_SELECTED)); ib1.setImageDrawable(ib1.getDrawable()); } else if (event.getAction() == MotionEvent.ACTION_UP) { ib1.getDrawable().clearColorFilter(); ib1.getDrawable().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED)); ib1.setImageResource(R.drawable.icon2); } return false; } }); }