如果您对setColorFilter(0xFF000000, Mode.MULTIPLY)
感到满意,那么如何让自己成为扩展ImageButton的自定义组件?
喜欢的东西:
(对不起,我还没有机会测试此)
package com.example;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
public class IosImageButton extends ImageButton implements OnTouchListener
{
public IosImageButton(final Context context, final AttributeSet attrs, final int defStyle)
{
super(context, attrs, defStyle);
this.init();
}
public IosImageButton(final Context context, final AttributeSet attrs)
{
super(context, attrs);
this.init();
}
public IosImageButton(final Context context)
{
super(context);
this.init();
}
private void init()
{
super.setOnTouchListener(this);
}
@Override
public boolean onTouch(final View view, final MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
// touchDown event -> Change color overlay. goes here
// e.g. this.setColorFilter(0xFF000000, Mode.MULTIPLY);
return true;
case MotionEvent.ACTION_UP:
// touchUp event -> remove color overlay, and perform button action.
// e.g. this.setColorFilter(0xFF000000, Mode.MULTIPLY);
return true;
default:
return false;
}
}
}
然后view.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=".MainActivity" >
<com.example.IosImageButton
<!-- add standard ImageButton setting here -->
/>
</RelativeLayout>