我想把阴影应用到ImageView.当我将阴影应用于TextView我正在获得它但同样它没有达到ImageView.我怎么解决这个问题?
可能重复:
如何在android中将阴影设置为View?
在我的Android应用程序中,我需要一个带圆角的布局,背景应该有一些透明度.为此,我使用这个xml,它工作正常:
customshape.xml:
<shape android:shape="rectangle">
<gradient android:startColor="#E2FFFFFF" android:endColor="#E2D0E3E5" android:angle="270"/>
<corners android:radius="10dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)
style.xml:
<style name="LinearLayoutRoundCorners">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">@drawable/customshape</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我还需要在布局的右侧和底部有一个小阴影.我找不到办法做到这一点.我已经看到了一个黑客,你把另一个布局放在后面,使它看起来像一个阴影,但这对我不起作用,因为我的布局有透明度,所以看起来不会很好.如何在不使用图像作为布局背景的情况下在布局上获得漂亮的阴影效果?
我发现如何改变a的不透明度View,但我需要实际上变暗a View.我最好的想法是在它上面放一个透明的黑色矩形,然后慢慢增加矩形的不透明度.
你知道更好的方法吗?
public class Page07AnimationView extends ParentPageAnimationView {
private final String TAG = this.getClass().getSimpleName();
private ImageView overlay;
private int mAlpha = 0;
public Page07AnimationView(Context context) {
super(context);
}
public Page07AnimationView(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void init()
{
overlay = new ImageView(mContext);
overlay.setImageResource(R.drawable.black_background);
overlay.setAlpha(0);
overlay.setWillNotDraw(false);
// make the PageAniSurfaceView focusable so it can handle events
setFocusable(true);
}
protected void draw_bitmaps(Canvas canvas)
{
overlay.draw(canvas);
update_bitmaps();
invalidate();
}
public void update_bitmaps()
{
if(mAlpha < …Run Code Online (Sandbox Code Playgroud) 我画了一个带有画布的矩形,我想知道是否有任何属性或方式给出一个小阴影.
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint pincel1 = new Paint();
pincel1.setColor(Color.rgb(151, 217, 69));
RectF rectangle = new RectF(30, 20,200,100);
canvas.drawRoundRect (rectangle, 6, 6, pincel1);
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我想进行扩展,LinearLayout以便在绘制布局时在其下方添加一个投影.我玩过这种onDraw方法但是我有点迷失了.任何有关这个甚至图书馆建议的帮助将不胜感激!
这是我想要实现的投影视图的示例.我不相信我可以在这里使用九个补丁因为我需要视图的内容在白框内.这意味着我需要知道边界与PNG末端之间的距离.但是我相信不同的屏幕密度意味着这个距离将始终是相同的PX但不是相同的DP.
所以要清楚我需要一种方法来扩展View类,以便在将它添加到布局时在其下面绘制阴影.请不要使用XML或9Patch解决方案.

谢谢
插口
我正在尝试做的事情将更好地与示例图像.正如你在下面看到的那样,我有一个灰色的背景,它的顶部是一个容器,里面有一些包含图像的填充物.容器也有轻微的阴影.
我想知道的是,如果在layout.xml中这样做非常费力吗?在普通的HTML文档中,这很简单.但由于这是针对移动应用程序以及许多屏幕分辨率等等,因此证明有点困难.
有什么建议?

编辑:我最终决定使用9patch图像.Everyting在创建过程中非常流畅但是当我在我的应用程序中实际使用它时,我会在图像的右侧和底部看到这些暗条纹.这些阴影似乎有效,它是一个非常浅的阴影..但那些条纹?

代码:
<de.hdodenhof.circleimageview.CircleImageView
android:padding="5dp"
android:background="@drawable/sub_cat_background"
android:layout_width="70dp"
android:layout_height="70dp"
android:id="@+id/tab_image"
android:src="@drawable/mehendi_tab"
android:layout_gravity="center_horizontal"/>
Run Code Online (Sandbox Code Playgroud)
sub_cat_background.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="2"
android:useLevel="false" >
<solid android:color="@android:color/transparent" />
<stroke
android:width="5dp"
android:color="@color/white" />
</shape>
Run Code Online (Sandbox Code Playgroud)
这是我在群众建议后得到的:
编辑4:
我也尝试过使用画布方式。
为此,我没有使用 xml 设置白色圆圈,而是使用带有白色圆圈的图像,如上所示(图 2)。
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.salon_selected);
int imageMaxSize = Math.max(bitmap.getWidth(), bitmap.getHeight());
RadialGradient gradient = new RadialGradient(imageMaxSize / 2, imageMaxSize / 2, imageMaxSize / 2,
new int[] {0xFFFFFFFF, 0xFFFFFFFF, 0x00FFFFFF},
new float[] {0.0f, 0.8f, 1.0f},
android.graphics.Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(gradient);
Canvas canvas=new Canvas();
// in onDraw(Canvas)
canvas.drawBitmap(bitmap, 0.0f, …Run Code Online (Sandbox Code Playgroud) 我目前正在使用包含ImageViews的GridView,并希望我的图像背后有阴影.例如,在网格中可能有15个图像随时可见.在假设我希望我的屏幕以50fps渲染以显得平滑的情况下工作,我的数学表明我希望每个ImageView的总绘制时间不会差于大约1.3ms.
我看了一下Romain Guy如何在他的Shelves应用程序中做阴影:http: //code.google.com/p/shelves/source/browse/trunk/Shelves/src/org/curiouscreature/android/shelves/util/ ImageUtilities.java
这似乎有道理,所以我创建了以下类:
public class ShadowImageView extends ImageView {
private static final int SHADOW_RADIUS = 8;
private static final int SHADOW_COLOR = 0x99000000;
private static final Paint SHADOW_PAINT = new Paint();
static {
SHADOW_PAINT.setShadowLayer(SHADOW_RADIUS / 2.0f, 0.0f,
0.0f, SHADOW_COLOR);
SHADOW_PAINT.setColor(0xFF000000);
SHADOW_PAINT.setStyle(Paint.Style.FILL);
}
public ShadowImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onDraw(Canvas canvas) {
final int containerWidth = getMeasuredWidth();
final int containerHeight = getMeasuredHeight();
canvas.drawRect(
SHADOW_RADIUS / 2.0f,
SHADOW_RADIUS / 2.0f,
containerWidth …Run Code Online (Sandbox Code Playgroud) android android-ui android-listview android-imageview android-gridview
如果我通过代码绘制圆形矩形,我可以使用setShadowLayer来获取形状的阴影.在XML中定义形状时是否存在等价物?
以下示例将圆形rect背景绘制为形状.我需要添加什么才能将阴影添加到形状中?甚至可以使用XML吗?
shape_test.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#8067BF6A"/>
<stroke android:width="3dp" android:color="#80000000" />
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp" />
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:background="#ffdddddd"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:background="@drawable/shape_test"
android:padding="5dp"
android:textStyle="bold"
android:textSize="28sp"
android:text="Hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud) android ×10
shadow ×3
layout ×2
android-ui ×1
android-view ×1
animation ×1
canvas ×1
glow ×1
imageview ×1
inheritance ×1
java ×1
shape ×1
textview ×1
view ×1
xml ×1