带有下拉阴影的自定义图像视图好吧,我一直在阅读和搜索,现在我的头撞在墙上,试图找出这一点。到目前为止,我的情况如下: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);}@Overrideprotected void onDraw(Canvas canvas) {
Rect r = mRect;
Paint paint = mPaint;
canvas.drawRect(r, paint);
super.onDraw(canvas);}@Overrideprotected 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);}}测量中的“+5”是暂时的;据我所知,我需要做一些数学来确定下降阴影添加到画布中的大小,对吗?但当我用这个: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;}在我的ImageView中,当我运行程序时,我仍然只得到一个普通的ImageView。有什么想法吗?谢谢。
3 回答
哆啦的时光机
TA贡献1779条经验 获得超6个赞
<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" />
萧十郎
TA贡献1815条经验 获得超13个赞
Paint mShadow = new Paint(); // radius=10, y-offset=2, color=black mShadow.setShadowLayer(10.0f, 0.0f, 2.0f, 0xFF000000); // in onDraw(Canvas) canvas.drawBitmap(bitmap, 0.0f, 0.0f, mShadow);
希望这能帮上忙。
注记
不要忘记蜂窝和上面你需要调用
setLayerType(LAYER_TYPE_SOFTWARE, mShadow)
,否则你将看不到你的影子!(@Dmitriy_Boichenko)
SetShadowLayer
不幸的是,硬件加速不能工作,因此它大大降低了性能
添加回答
举报
0/150
提交
取消