OnTouch应用光晕效果图像的中心 [英] OnTouch Apply glow effect to center of image
问题描述
我有一个形象,我需要发光效果适用于使用OnTouchListener图片中心
I have a image and I need to apply glow effect to center of the image using OnTouchListener
这样的事情。
我怎样才能达到这样的效果?我已经调查的例子,我们可以应用发光效果的图像外的部分。
How can I achieve this effect? I have looked into examples where we can apply glow effect to outer part of the image.
我们可以通过使用白色图像和顶部的背景图像的地方达到这个目的,但我们可以做到这一点,而不使用的图像?
We can achieve this by using a white image and place on top the background image, but can we do it without using images?
修改
我发现这个职位很好,但没有办法了。
I found this post as well but no solution.
推荐答案
在 setOnTouchListener
获得 getDrawingCache()
的图像,创建你想要什么的梯度位图,然后在彼此顶部重叠显示图像
On the setOnTouchListener
get the getDrawingCache()
of the image, Create a gradient bitmap of what you want and then overlay the images on top of one another
试试这个
final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if(drawIt){
drawIt = false;
//Build the cache
imageView.buildDrawingCache();
Bitmap original = imageView.getDrawingCache();
//Build the gradient
Bitmap gradient = getGradient();
//Overlay the images
Bitmap finalImage = overlay(original,gradient,event.getX(),event.getY());
imageView.setImageBitmap(finalImage);
}
break;
case MotionEvent.ACTION_UP:
drawIt = true;
break;
}
return false;
}
});
private Bitmap getGradient() {
RadialGradient gradient = new RadialGradient(200 , 200, 200, 0xFFFFFFFF,
0x00000000, android.graphics.Shader.TileMode.CLAMP);
Paint p = new Paint();
p.setDither(true);
p.setShader(gradient);
Bitmap bitmap = Bitmap.createBitmap(400, 400, Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
c.drawCircle(200, 200, 200, p);
return Bitmap.createScaledBitmap(bitmap, 50, 50, false);
}
private Bitmap overlay(Bitmap bmp1, Bitmap bmp2,float x, float y) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
// Use your x and y cordinates here
canvas.drawBitmap(bmp2, 100,100, null);
return bmOverlay;
}
下面是我得到
您可以做您的修改在这里,我上传项目适合你。
You can do your modifications here i have uploaded the project for you
祝您好运
的 修改的
EDIT
要在中心绘制它使用此行的覆盖
法
To draw it at center use this line in the overlay
method
canvas.drawBitmap(bmp2, bmp1.getWidth()/2 - bmp2.getWidth()/2,bmp1.getHeight()/2 - bmp2.getHeight()/2, null);
和添加位图按钮使用这种
And for adding bitmap to button use this
Button btn = (Button) findViewById(R.id.button1);
Drawable d = new BitmapDrawable(getResources(),finalImage);
btn.setBackgroundDrawable(d);
但需要注意的是,当你设置此位图按钮,该按钮将调整所以最好不要用 WRAP_CONTENT
,但指定硬code中的高度和宽度,如 25dp
或 100dp
这是你要控制逻辑
But note that when you set this bitmap to button the button will resize so better not use wrap_content
but specify the height and width in hard code like 25dp
or 100dp
etc. That is the logic which you have to control
您也可以使用的ImageButton ,并把它设置为
You can also use ImageButton and set it as
btn.setImageBitmap(finalImage);
对于MotionEvent
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
drawIt = true;
break;
case MotionEvent.ACTION_UP:
if(drawIt){
//Build the cache
imageView.buildDrawingCache();
Bitmap original = imageView.getDrawingCache();
//Build the gradient
Bitmap gradient = getGradient();
//Overlay the images
Bitmap finalImage = overlay(original,gradient,event.getX(),event.getY());
imageView.setImageBitmap(finalImage);
Button btn = (Button) findViewById(R.id.button1);
Drawable d = new BitmapDrawable(getResources(),finalImage);
btn.setBackgroundDrawable(d);
}
break;
case MotionEvent.ACTION_CANCEL:
drawIt = false;
break;
}
的 修改2 的
EDIT 2
声明这些实例变量
private boolean drawIt = true;
Button btn1,btn2;
int x_limit, y_limit;
Bitmap bmpOrignal, bmpGradient, bmpOverlay;
和code中的 onTouch
像这样
@Override
public boolean onTouch(View v, MotionEvent event) {
if(drawIt){
drawIt = false;
v.buildDrawingCache();
bmpOrignal = v.getDrawingCache();
bmpGradient = getGradient();
bmpOverlay = overlay(bmpOrignal,bmpGradient);
x_limit = v.getLeft() + bmpOrignal.getWidth();
y_limit = v.getTop() + bmpOrignal.getHeight();
}
if(event.getX() > x_limit || event.getY() > y_limit){
((Button)v).setBackgroundDrawable(new BitmapDrawable(getResources(),bmpOrignal));
}else {
((Button)v).setBackgroundDrawable(new BitmapDrawable(getResources(),bmpOverlay));
}
if(event.getAction() == MotionEvent.ACTION_UP){
drawIt = true ;
}
return false;
}
您可以下载新的更新的项目这里
You can download the new updated project from here
这篇关于OnTouch应用光晕效果图像的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!