view中显示部分区域
在android中有时候要求只显示view的部分区域,这个时候就需要对当前的view进行剪裁的操作。在android中剪裁当前的view的有两种方法:一种是直接截取view,第二种是通过Outline设置。涉及到的类方法如下:
- View.getGlobalVisibleRect(Rect rect)
- View.setClipBounds(Rect rect)
- View.setOutlineProvider()
- View.setClipToOutline(boolean clipToOutline)
getGlobalVisibleRect(Rect rect):获取当前view的可视区域,坐标系使用的Root View的也就是DecorView的坐标系,这点需要注意,不是View自身的坐标系。
setClipBounds(Rect rect),直接指定当前view的可视区域,当前的Rect使用的view的自身的坐标系。
setOutlineProvider(),设置当前View的Outline。
setClipToOutlines(),截取当前的可视区域到Outline,如果设置setClipBounds()方法,这个方法就失效了。
下面通过一个简单的例子来说明一下这几个方法的使用:
MainActivity.java
package com.app.motiongear.clipboundsmotion; import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button mRectBtn, mBoundBtn;
ImageView mImageView;
Rect originRect = new Rect(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mRectBtn = (Button) this.findViewById(R.id.btn1);
mBoundBtn = (Button) this.findViewById(R.id.btn2);
mImageView = (ImageView) this.findViewById(R.id.imageview);
mRectBtn.setOnClickListener(this);
mBoundBtn.setOnClickListener(this);
mImageView.setOutlineProvider(new CustomOutlineProvider());
} @Override
public void onClick(View v) {
if (v == mRectBtn) {
//getGlobalVisibleRect()相对与父布局的rect
mImageView.getGlobalVisibleRect(originRect);
int centerX = (originRect.right - originRect.left) / 2;
int centerY = (originRect.bottom - originRect.top) / 2;
//设置View的显示区域,坐标是自身
Rect tmp = new Rect(centerX - 150, centerY - 150, centerX + 150, centerY + 150);
mImageView.setClipBounds(tmp);
}
if (v == mBoundBtn) {
//通过Outline设置
if(!mImageView.getClipToOutline()){
mImageView.setClipToOutline(true);
}else{
mImageView.setClipToOutline(false);
} }
}
}
CustomOutlineProvider.java
package com.app.motiongear.clipboundsmotion; import android.graphics.Outline;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewOutlineProvider; /**
* Created by ubuntu on 15-11-13.
*/
public class CustomOutlineProvider extends ViewOutlineProvider { @Override
public void getOutline(View view, Outline outline) {
Rect rect = new Rect();
view.getGlobalVisibleRect(rect);
int leftMargin =100;
int topMargin = 100;
Rect selfRect = new Rect(leftMargin, topMargin,
rect.right - rect.left - leftMargin, rect.bottom - rect.top - topMargin);
outline.setRoundRect(selfRect, 30);
}
}
调用clipToBounds()方法:

调用clipToOutline()方法:

view中显示部分区域的更多相关文章
- 如何让同一个字段在不同的view中显示不同的内容
many2one 字段默认显示 对象的name字段, 但也可以定义name_get方法显示不同的内容 如res.partner 对象可以根据 context 内容是否显示 客户的地址,职位,email ...
- 将Controller中的数据传递到View中显示
如何将Controller 中的数据传送到View 步骤: (1)要有数据,如果要用到对象可以在Model 中定义对应的类 (2)要有装数据的容器: System.Text.StringBuilder ...
- odoo学习之:在tree view中显示部分数据domain的使用
只要在window.action中他添加相应东domain即可,如: <!-- 树型列表 --> <record model="ir.actions.act_window& ...
- 多表的时候怎样在MVC VIEW中显示
原文发布时间为:2011-04-01 -- 来源于本人的百度文章 [由搬家工具导入] Linq join query displayed in MVC view Instead of returnin ...
- 得到ImageView中drawable显示的区域的计算方法
我们都知道Imageview中有不同的拉伸比率,比如fitStart,centCrop这样的,所以imageview中的drawable不一定和imageview占有相同的位置和大小,那么怎么计算呢? ...
- (ios7) 解决Ios7中,Navigatebar 显示在主View中,和ios6 不一致问题
在ios 7 系统中 NavigateBar 显示在主View中 ,Ios6 不在主View中,导致后台代码的方式编写View不一致 解决方法: 在ViewControl的viewDidLoad 方法 ...
- 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常
代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常 设置 padding=0
- View中的Razor使用
View中的Razor使用 上一节:ASP.NET MVC5 + EF6 入门教程 (5) Model和Entity Framework 源码下载:点我下载 一.Razor简介 在解决方案资源管理 ...
- Activtiy完全解析(三、View的显示过程measure、layout、draw)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/52840065 本文出自:[openXu的博客] 在Activity完全解析的第一篇文章A ...
随机推荐
- 程序员都在用的 IDEA 插件(不断更新)
IDEA一些不错的插件分享 目录 IDEA一些不错的插件分享 插件集合 CamelCase Translation LiveEdit MarkDown Navigator Jrebel CheckSt ...
- Spring IOC实现配置bean和实例
配置 beans.xml文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&quo ...
- Linux下搭建mongDB环境
参考: https://blog.csdn.net/qq_35763837/article/details/79654023 https://www.linuxidc.com/Linux/2016-0 ...
- 一文带你了解nginx基础
学习nginx,就要先了解什么是nginx,为什么使用nginx,最后才是了解怎么使用nginx nginx简介 nginx安装 一.Linux中安装nginx 二.Docker中安装nginx 三. ...
- 读Pyqt4简介,带你入门Pyqt4 _001
PyQt是用来创建GUI应用程序的工具包,它把Python和成功的Qt绑定在一起,Qt库是这个星球上最强大的库之一,如果不是最强大的话. PyQt作为一组Python模块的实现.有超过300个类和超过 ...
- python报错2
缩进导致的报错 IndentationError: unindent does not match any outer indentation level NameError 命名错误 原因是: na ...
- 获取元素节点的子节点 & 获取文本节点
1. 获取元素节点的子节点(**只有元素节点才有子节点): ①. childNodes 属性获取全部的子节点, 但该方法不实用. 因为如果要获取指定的节点 ...
- python3程序开发指南——第1章 笔记
python文件的扩展名为.py ,但是python GUI程序的扩展名为.pyw 在python中,注释以#开始,作用范围为该行 IDLE提供了三个关键功能:输入python表达式与代码,并在pyt ...
- abp(net core)+easyui+efcore实现仓储管理系统——出库管理之一(四十九)
abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...
- Alpha冲刺——总结随笔
这个作业属于哪个课程 软件工程 这个作业要求在哪里 团队作业第五次--Alpha冲刺 这个作业的目标 Alpha冲刺 作业正文 正文 github链接 项目地址 其他参考文献 无 一.项目预期计划: ...