Fading the image border in an imageview

2019-06-22 05:25发布

I need to add a fading effect on a ImageView. In my situation I have a RelativeLayout, the relative background in yellow and in the center of that background there is a ImageView. Now I need to apply a fade affect on the edge of the ImageView. Is this possible? (Android api 11+)

I tried:

android:requiresFadingEdge="horizontal"
android:fadingEdge="horizontal"
android:fadingEdge="vertical"
android:fadingEdgeLength="40px"

but doesn't work..

enter image description here

This is an example of what I want. Like you can see the top and the left are fading, the right and bottom edge, instead, are not fading..

Thanks a lot!

2条回答
倾城 Initia
2楼-- · 2019-06-22 05:56

Override methods in ImageView as written below:

private float FadeStrength = 1.0f;
private int FadeLength = 40px;

@Override
protected float getTopFadingEdgeStrength() {
    return FadeStrength;
}

@Override
protected float getBottomFadingEdgeStrength() {
    return FadeStrength;
}

@Override
protected float getLeftFadingEdgeStrength() {
    return FadeStrength;
}

@Override
protected float getRightFadingEdgeStrength() {
    return FadeStrength;
}

And on initialization of view add lines:

setFadingEdgeLength(FadeLength);
setVerticalFadingEdgeEnabled(true);
setHorizontalFadingEdgeEnabled(true);
查看更多
走好不送
3楼-- · 2019-06-22 06:00

Try this work around: Don't need to extend ImageView

imageView.scrollTo(1,0);
imageView.setHorizontalFadingEdgeEnabled(true);
imageView.setFadingEdgeLength(40);
查看更多
登录 后发表回答