自定义的ImageView与阴影 [英] Custom ImageView with drop shadow
问题描述
好吧,我一直在阅读和摸索,而我现在敲我的头撞在墙上试图找出了这一点。这是我到目前为止有:
包com.pockdroid.sandbox;
进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.graphics.Rect;
进口android.widget.ImageView;
公共类ShadowImageView扩展ImageView的{
私人矩形mRect;
私人油漆mPaint;
公共ShadowImageView(上下文的背景下)
{
超(上下文);
mRect =新的矩形();
mPaint =新的油漆();
mPaint.setAntiAlias(真正的);
mPaint.setShadowLayer(2F,1F,1F,Color.BLACK);
}
@覆盖
保护无效的OnDraw(帆布油画)
{
矩形R = mRect;
涂料粉刷= mPaint;
canvas.drawRect(R,漆);
super.onDraw(画布);
}
@覆盖
保护无效onMeasure(INT W,INT高)
{
super.onMeasure(W,H);
INT mH为毫瓦;
毫瓦= getSuggestedMinimumWidth()&其中; getMeasuredWidth()? getMeasuredWidth():getSuggestedMinimumWidth();
毫亨= getSuggestedMinimumHeight()&其中; getMeasuredHeight()? getMeasuredHeight():getSuggestedMinimumHeight();
setMeasuredDimension(MW + 5,MH + 5);
}
}
在测量的+5是有作为临时的;据我了解,我需要做一些数学来确定的阴影添加到画布的大小,对不对?
但是当我使用这样的:
公开查看getView(INT位置,查看convertView,ViewGroup中父){
ShadowImageView sImageView;
如果(convertView == NULL){
sImageView =新ShadowImageView(mContext);
GridView.LayoutParams LP =新GridView.LayoutParams(85,85);
sImageView.setLayoutParams(LP);
sImageView.setScaleType(ImageView.ScaleType.CENTER);
sImageView.setPadding(5,5,5,5);
} 其他 {
sImageView =(ShadowImageView)convertView;
}
sImageView.setImageBitmap(bitmapList.get(位置));
返回sImageView;
}
在我的ImageView,我仍然得到的只是一个普通的ImageView当我运行该程序。
有什么想法?谢谢你。
编辑:所以我在IRC频道采访了RomainGuy一些,我有它的工作现在普通的矩形图像下面code。它仍然不会直接绘制阴影,以我的位图的透明的,所以我还在上。
@覆盖
保护无效的OnDraw(帆布油画)
{
BMP位= BitmapFactory.de codeResource(getResources(),R.drawable.omen);
涂料粉刷=新的油漆();
paint.setAntiAlias(真正的);
paint.setShadowLayer(5.5F,6.0f,6.0f,Color.BLACK);
canvas.drawColor(Color.GRAY);
canvas.drawRect(50,50,50 + bmp.getWidth(),50 + bmp.getHeight(),漆);
canvas.drawBitmap(BMP,50,50,NULL);
}
好吧,我不预见这一个更多的答案,所以我结束了去了,现在只需矩形图像的解决方案。我用下面的NinePatch:
随着XML适当的填充:
< ImageView的
机器人:ID =@ + ID / image_test
机器人:背景=@可绘制/ drop_shadow
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:以下属性来=6px
机器人:paddingTop =递四方
机器人:paddingRight =8像素
机器人:paddingBottom会=9px
机器人:SRC =@可绘制/ PIC1
/>
获得了相当不错的成绩:
不理想,但它会做。
Okay, I've been reading and searching around, and am now banging my head against the wall trying to figure this out. Here's what I have so far:
package com.pockdroid.sandbox;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.widget.ImageView;
public class ShadowImageView extends ImageView {
private Rect mRect;
private Paint mPaint;
public ShadowImageView(Context context)
{
super(context);
mRect = new Rect();
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setShadowLayer(2f, 1f, 1f, Color.BLACK);
}
@Override
protected void onDraw(Canvas canvas)
{
Rect r = mRect;
Paint paint = mPaint;
canvas.drawRect(r, paint);
super.onDraw(canvas);
}
@Override
protected void onMeasure(int w, int h)
{
super.onMeasure(w,h);
int mH, mW;
mW = getSuggestedMinimumWidth() < getMeasuredWidth()? getMeasuredWidth() : getSuggestedMinimumWidth();
mH = getSuggestedMinimumHeight() < getMeasuredHeight()? getMeasuredHeight() : getSuggestedMinimumHeight();
setMeasuredDimension(mW + 5, mH + 5);
}
}
The "+5" in the measurements are there as temporary; From what I understand I'll need to do some math to determine the size that the drop shadow adds to the canvas, right?
But when I use this:
public View getView(int position, View convertView, ViewGroup parent) {
ShadowImageView sImageView;
if (convertView == null) {
sImageView = new ShadowImageView(mContext);
GridView.LayoutParams lp = new GridView.LayoutParams(85, 85);
sImageView.setLayoutParams(lp);
sImageView.setScaleType(ImageView.ScaleType.CENTER);
sImageView.setPadding(5,5,5,5);
} else {
sImageView = (ShadowImageView) convertView;
}
sImageView.setImageBitmap(bitmapList.get(position));
return sImageView;
}
in my ImageView, I still get just a normal ImageView when I run the program.
Any thoughts? Thanks.
EDIT: So I spoke with RomainGuy some in the IRC channel, and I have it working now for plain rectangular images with the below code. It still won't draw the shadow directly to my bitmap's transparency though, so I'm still working on that.
@Override
protected void onDraw(Canvas canvas)
{
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.omen);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShadowLayer(5.5f, 6.0f, 6.0f, Color.BLACK);
canvas.drawColor(Color.GRAY);
canvas.drawRect(50, 50, 50 + bmp.getWidth(), 50 + bmp.getHeight(), paint);
canvas.drawBitmap(bmp, 50, 50, null);
}
Okay, I don't foresee any more answers on this one, so what I ended up going with for now is just a solution for rectangular images. I've used the following NinePatch:
along with the appropriate padding in XML:
<ImageView
android:id="@+id/image_test"
android:background="@drawable/drop_shadow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="6px"
android:paddingTop="4px"
android:paddingRight="8px"
android:paddingBottom="9px"
android:src="@drawable/pic1"
/>
to get a fairly good result:
Not ideal, but it'll do.
这篇关于自定义的ImageView与阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!