• 一手遮天 Android


    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

    一手遮天 Android - view(基础): 剪裁

    示例如下:

    /view/ViewDemo3.java

    /**
     * 演示 view 剪裁
     */
    
    package com.webabcd.androiddemo.view;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.graphics.Outline;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewOutlineProvider;
    import android.widget.ImageView;
    
    import com.webabcd.androiddemo.R;
    
    public class ViewDemo3 extends AppCompatActivity {
    
        private ImageView _imageView1;
        private ImageView _imageView2;
        private ImageView _imageView3;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_view_viewdemo3);
    
            _imageView1 = findViewById(R.id.imageView1);
            _imageView2 = findViewById(R.id.imageView2);
            _imageView3 = findViewById(R.id.imageView3);
    
            sample1();
            sample2();
            sample3();
        }
    
        // 圆形剪裁
        private void sample1() {
            ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
                @Override
                public void getOutline(View view, Outline outline) {
                    outline.setOval(0,0,_imageView1.getWidth(), _imageView1.getHeight());
                }
            };
            _imageView1.setOutlineProvider(viewOutlineProvider);
            _imageView1.setClipToOutline(true);
        }
    
        // 矩形剪裁
        private void sample2() {
            ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
                @Override
                public void getOutline(View view, Outline outline) {
                    outline.setRect(20,0,_imageView2.getWidth(), _imageView2.getHeight());
                }
            };
            _imageView2.setOutlineProvider(viewOutlineProvider);
            _imageView2.setClipToOutline(true);
        }
    
        // 圆角剪裁
        private void sample3() {
            ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
                @Override
                public void getOutline(View view, Outline outline) {
                    outline.setRoundRect(0,0,_imageView3.getWidth(), _imageView3.getHeight(), 20);
                }
            };
            _imageView3.setOutlineProvider(viewOutlineProvider);
            _imageView3.setClipToOutline(true);
        }
    }
    

    /layout/activity_view_viewdemo3.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/img_sample_son"/>
    
        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/img_sample_son"/>
    
        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/img_sample_son"/>
    
    </LinearLayout>
    
    

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

  • 相关阅读:
    Android布局与事件
    android 开发中共享全局数据Application的类
    从Android界面开发谈起【转】
    HttpClient java
    关于整合S&K框架的一些个小注意
    批处理执行多条语句
    Atitit. 软件设计 模式 变量 方法 命名最佳实践 vp820 attilax总结命名表大全
    atitit.软件设计模式大的总结attialx总结
    Atitit.软件开发的最终的设计 dsl化,ast化(建立ast, 解析执行ast)
    atitit。流程图的设计与制作 attilax 总结
  • 原文地址:https://www.cnblogs.com/webabcd/p/android_view_ViewDemo3.html
Copyright © 2020-2023  润新知