当前位置: 代码迷 >> Android >> Android Outline跟Clip
  详细解决方案

Android Outline跟Clip

热度:101   发布时间:2016-04-27 23:12:03.0
Android Outline和Clip

1

可以使用ViewOutlineProvider实现Outline和Clip

public class MainActivity extends Activity {    private ImageView mImageView;    private Rect mViewRect;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mImageView = (ImageView) findViewById(R.id.image);        mImageView.setOutlineProvider(mOutlineProvider);        mImageView.setClipToOutline(true);//裁剪    }    ViewOutlineProvider mOutlineProvider = new ViewOutlineProvider() {        @Override        public void getOutline(View view, Outline outline) {            if(mViewRect != null){                //outline.setRoundRect(mViewRect, 15);//绘制轮廓,圆角                outline.setRect(mViewRect);                //outline.canClip();//是否符合裁剪            }else{                outline.setRoundRect(new Rect(0, 0, mImageView.getWidth(), mImageView.getHeight()),15);            }        }    };    public void clickButton1(View view){        mViewRect = new Rect(0, 0, mImageView.getWidth()/2, mImageView.getHeight());        mImageView.invalidateOutline();    }    public void clickButton2(View view){        mViewRect = new Rect(0, 0, mImageView.getWidth(), mImageView.getHeight()/2);        mImageView.invalidateOutline();    }    public void clickButton3(View view){        mViewRect = new Rect(mImageView.getWidth()/6, mImageView.getHeight()/8, mImageView.getWidth()*5/6, mImageView.getHeight()*7/8);        mImageView.invalidateOutline();    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

Shadow效果以及Clip的三种效果
圆角效果Clip1Clip2Clip3

版权声明:本文为博主原创文章,未经博主允许不得转载。

  相关解决方案