how to hide glow effect?
Вопрос
Glow effect is working fine. my doubt is how to hide glow effect? if i click my imageview, that time only i wish to show my glow effect please how to hide and show glow effect while on click.
code:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// An added margin to the initial image
int margin = 24;
int halfMargin = margin / 2;
// the glow radius
int glowRadius = 16;
// the glow color
int glowColor = Color.rgb(0, 192, 255);
// The original image to use
Bitmap src = BitmapFactory.decodeResource(getResources(),
R.drawable.test);
// extract the alpha from the source image
Bitmap alpha = src.extractAlpha();
// The output bitmap (with the icon + glow)
Bitmap bmp = Bitmap.createBitmap(src.getWidth() + margin,
src.getHeight() + margin, Bitmap.Config.ARGB_8888);
// The canvas to paint on the image
Canvas canvas = new Canvas(bmp);
Paint paint = new Paint();
paint.setColor(glowColor);
// outer glow
paint.setMaskFilter(new BlurMaskFilter(glowRadius, Blur.OUTER));
canvas.drawBitmap(alpha, halfMargin, halfMargin, paint);
// original icon
canvas.drawBitmap(src, halfMargin, halfMargin, null);
setContentView(R.layout.activity_main);
((ImageView) findViewById(R.id.bmpImg)).setImageBitmap(bmp);
}
}
present screen shot:
Решение
set onclicklistener and implement this code:
.setOnClickListener(clicklistener);
private OnClickListener backListener = new OnClickListener() {
public void onClick(View v) {
// An added margin to the initial image
int margin = 24;
int halfMargin = margin / 2;
// the glow radius
int glowRadius = 16;
// the glow color
int glowColor = Color.rgb(0, 192, 255);
// The original image to use
Bitmap src = BitmapFactory.decodeResource(getResources(),
R.drawable.test);
// extract the alpha from the source image
Bitmap alpha = src.extractAlpha();
// The output bitmap (with the icon + glow)
Bitmap bmp = Bitmap.createBitmap(src.getWidth() + margin,
src.getHeight() + margin, Bitmap.Config.ARGB_8888);
// The canvas to paint on the image
Canvas canvas = new Canvas(bmp);
Paint paint = new Paint();
paint.setColor(glowColor);
// outer glow
paint.setMaskFilter(new BlurMaskFilter(glowRadius, Blur.OUTER));
canvas.drawBitmap(alpha, halfMargin, halfMargin, paint);
// original icon
canvas.drawBitmap(src, halfMargin, halfMargin, null);
}}
Другие советы
you can set null for setMaskFilter()
like this way
paint.setMaskFilter(null);
and you need just set paint.setMaskFilter(new BlurMaskFilter(glowRadius, Blur.OUTER));
for this you need keep paint object for application scope so this paint object can accessing out in another class or activity wherever you want or you can set flag for true then show glow effect and false the flag set nothing (as default)
An easier way to do this would be to use StateListDrawable
.
Create two images - one for normal state and one for pressed state (with glow). Use this in the res/drawable/button_drawable.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/image_normal"
android:state_enabled="true"/>
<item
android:drawable="@drawable/image_pressed"
android:state_pressed="true"/>
</selector>
and use it as the buttom drawable:
((ImageView) findViewById(R.id.bmpImg)).setImageDrawable(getResources().getDrawable(R.drawable.button_drawable));