不用自定义view就能实现的效果(渐变,镜子,直线,圆圈等)

在drawable里面新创建一个 Drawable resource:

1.绘制一条直线/虚线

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke android:color="@color/colorPrimary" android:width="5dp"></stroke>
</shape>

效果图:

虚线:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line"
    >
    <stroke
        android:color="@color/colorPrimary"
        android:width="5dp"
        android:dashWidth="10dp"
        android:dashGap="5dp"></stroke>
</shape>

效果图

2.绘制一个两种颜色渐变的圆圈:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    >
    
    <stroke android:color="@color/colorPrimary"></stroke>
    <size android:width="40dp" android:height="40dp"></size>

    <gradient android:startColor="@color/colorPrimary"
        android:endColor="@color/colorAccent"></gradient>

</shape>

效果图:

3.镜子效果:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/pic1"
    android:antialias="true"
    android:dither="true"
    android:filter="true"
    android:mipMap="true"
    android:tileMode="mirror"
    >

</bitmap>

这个的话在drawable里面的视图里面是看不到效果的,得实际用在界面上才能看出效果

注意:如果你发现你的代码没错但镜子效果出不来,不要着急,可能是因为你的图片太大了,把图片的像素改小点就OK了。

效果图:

下面的效果只要更改  android:tileMode  就可以了

android:tileMode="repeat"  平铺效果

扫描二维码关注公众号,回复: 4312558 查看本文章
android:tileMode="clamp"  拉伸效果

android:tileMode="disabled"  平铺效果

4.点击图片逐渐显示出来(从一个颜色逐渐过渡到另一个颜色):

挺想放效果的但是我不会。

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="@color/colorAccent"></solid>
        </shape>
    </item>

    <item>
        <shape>
            <solid android:color="@color/colorPrimary"></solid>
        </shape>
    </item>

</transition>

当然还要设置点击事件:

    private ImageView transition;
    private boolean isCheck;


final TransitionDrawable drawable = (TransitionDrawable) transition.getBackground();
        transition.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isCheck) {
                    drawable.startTransition(2000);
                    isCheck = true;
                } else {
                    drawable.reverseTransition(2000);
                    isCheck = false;
                }
            }
        });

5.从中间开始展示图片:

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:clipOrientation="horizontal"
    android:drawable="@color/colorPrimary"
    android:gravity="center"
    >
    clipOrientation 控制裁减的方向
    gravity 控制从哪里开始裁减
</clip>

布局文件:

    拉动展示背景图片
    <ImageView
        android:id="@+id/clip_drawable"
        android:background="@drawable/clip_drawable"
        android:layout_width="match_parent"
        android:layout_height="200dp" />
    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/seekbar"
        />

效果图:

附上代码:

    private ImageView clip_drawable;
    private SeekBar seekbar;

        seekbar.setMax(10000);
        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                clip_drawable.getBackground().setLevel(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

猜你喜欢

转载自blog.csdn.net/ykx_1448488568/article/details/84667547