- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
如何为 ImageView
赋予六边形形状。有可能以同样的方式做吗?如果是这样,那么如何。如果这是不可能的,那么如何实现呢?
<shape xmlns:android="http//schemas.android.com/apk/res/android"
android:shape="hexagon">
<solid android:color="#ffffffff" />
<size android:width="60dp"
android:height="40dp" />
</shape>
这里我不能做 mask 图像,因为我无法检测到我应该裁剪位图的哪个部分以获得六边形位图。所以我正在寻找将六边形形状赋予 ImageView
最佳答案
试试这个 View 。您可能希望根据您的特定需求对其进行调整,但它会在 View 顶部绘制一个带有边框的六边形蒙版。背景资源位于掩码下方。
结果:
代码:
HexagonMaskView.java
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Path;
import android.graphics.Region;
import android.util.AttributeSet;
import android.view.View;
public class HexagonMaskView extends View {
private Path hexagonPath;
private Path hexagonBorderPath;
private float radius;
private float width, height;
private int maskColor;
public HexagonMaskView(Context context) {
super(context);
init();
}
public HexagonMaskView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public HexagonMaskView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
hexagonPath = new Path();
hexagonBorderPath = new Path();
maskColor = 0xFF01FF77;
}
public void setRadius(float r) {
this.radius = r;
calculatePath();
}
public void setMaskColor(int color) {
this.maskColor = color;
invalidate();
}
private void calculatePath() {
float triangleHeight = (float) (Math.sqrt(3) * radius / 2);
float centerX = width/2;
float centerY = height/2;
hexagonPath.moveTo(centerX, centerY + radius);
hexagonPath.lineTo(centerX - triangleHeight, centerY + radius/2);
hexagonPath.lineTo(centerX - triangleHeight, centerY - radius/2);
hexagonPath.lineTo(centerX, centerY - radius);
hexagonPath.lineTo(centerX + triangleHeight, centerY - radius/2);
hexagonPath.lineTo(centerX + triangleHeight, centerY + radius/2);
hexagonPath.moveTo(centerX, centerY + radius);
float radiusBorder = radius - 5;
float triangleBorderHeight = (float) (Math.sqrt(3) * radiusBorder / 2);
hexagonBorderPath.moveTo(centerX, centerY + radiusBorder);
hexagonBorderPath.lineTo(centerX - triangleBorderHeight, centerY + radiusBorder/2);
hexagonBorderPath.lineTo(centerX - triangleBorderHeight, centerY - radiusBorder/2);
hexagonBorderPath.lineTo(centerX, centerY - radiusBorder);
hexagonBorderPath.lineTo(centerX + triangleBorderHeight, centerY - radiusBorder/2);
hexagonBorderPath.lineTo(centerX + triangleBorderHeight, centerY + radiusBorder/2);
hexagonBorderPath.moveTo(centerX, centerY + radiusBorder);
invalidate();
}
@Override
public void onDraw(Canvas c){
super.onDraw(c);
c.clipPath(hexagonBorderPath, Region.Op.DIFFERENCE);
c.drawColor(Color.WHITE);
c.save();
c.clipPath(hexagonPath, Region.Op.DIFFERENCE);
c.drawColor(maskColor);
c.save();
}
// getting the view size and default radius
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = MeasureSpec.getSize(widthMeasureSpec);
height = MeasureSpec.getSize(heightMeasureSpec);
radius = height / 2 - 10;
calculatePath();
}
}
29.07.2016 更新
一种更好的方法,只剪辑源图像而不绘制整个 View 的背景。切换到 ImageView 作为基类以受益于 scaleType。我还做了一些代码重构。
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.Region;
import android.util.AttributeSet;
import android.widget.ImageView;
public class HexagonMaskView extends ImageView {
private Path hexagonPath;
private Path hexagonBorderPath;
private Paint mBorderPaint;
public HexagonMaskView(Context context) {
super(context);
init();
}
public HexagonMaskView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public HexagonMaskView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
this.hexagonPath = new Path();
this.hexagonBorderPath = new Path();
this.mBorderPaint = new Paint();
this.mBorderPaint.setColor(Color.WHITE);
this.mBorderPaint.setStrokeCap(Paint.Cap.ROUND);
this.mBorderPaint.setStrokeWidth(50f);
this.mBorderPaint.setStyle(Paint.Style.STROKE);
}
public void setRadius(float radius) {
calculatePath(radius);
}
public void setBorderColor(int color) {
this.mBorderPaint.setColor(color);
invalidate();
}
private void calculatePath(float radius) {
float halfRadius = radius / 2f;
float triangleHeight = (float) (Math.sqrt(3.0) * halfRadius);
float centerX = getMeasuredWidth() / 2f;
float centerY = getMeasuredHeight() / 2f;
this.hexagonPath.reset();
this.hexagonPath.moveTo(centerX, centerY + radius);
this.hexagonPath.lineTo(centerX - triangleHeight, centerY + halfRadius);
this.hexagonPath.lineTo(centerX - triangleHeight, centerY - halfRadius);
this.hexagonPath.lineTo(centerX, centerY - radius);
this.hexagonPath.lineTo(centerX + triangleHeight, centerY - halfRadius);
this.hexagonPath.lineTo(centerX + triangleHeight, centerY + halfRadius);
this.hexagonPath.close();
float radiusBorder = radius - 5f;
float halfRadiusBorder = radiusBorder / 2f;
float triangleBorderHeight = (float) (Math.sqrt(3.0) * halfRadiusBorder);
this.hexagonBorderPath.reset();
this.hexagonBorderPath.moveTo(centerX, centerY + radiusBorder);
this.hexagonBorderPath.lineTo(centerX - triangleBorderHeight, centerY + halfRadiusBorder);
this.hexagonBorderPath.lineTo(centerX - triangleBorderHeight, centerY - halfRadiusBorder);
this.hexagonBorderPath.lineTo(centerX, centerY - radiusBorder);
this.hexagonBorderPath.lineTo(centerX + triangleBorderHeight, centerY - halfRadiusBorder);
this.hexagonBorderPath.lineTo(centerX + triangleBorderHeight, centerY + halfRadiusBorder);
this.hexagonBorderPath.close();
invalidate();
}
@Override
public void onDraw(Canvas c) {
c.drawPath(hexagonBorderPath, mBorderPaint);
c.clipPath(hexagonPath, Region.Op.INTERSECT);
c.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
super.onDraw(c);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(width, height);
calculatePath(Math.min(width / 2f, height / 2f) - 10f);
}
}
示例布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@android:color/holo_green_dark">
<com.scelus.hexagonmaskimproved.HexagonMaskView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/bear"
android:background="@android:color/holo_green_light"/>
</RelativeLayout>
关于android - 如何为 ImageView 赋予六边形形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22601400/
Solr中有什么方法可以给同义词赋予权重? (由 SynonymFilterFactory 生成) 问题的较长版本/一些背景: 我们希望为 SynonymFilterFactory 注入(inject
假设我有一个可以扩展的 A 类。在 A 类中,我有一个列表 List 。所以这个类将包含一个包含元素 A 的列表。现在,如果我将这个类 B 扩展为 A 的子类,我希望类 B 具有相同的成员 List
有没有办法让 SKNode 拥有自己的物理特性?我有一个 SKShapeNode 调用“backGround”,我将其用作大多数其他节点的父节点。我不断地将“背景”向左移动,给人一种玩家正在前进的错觉
我想观察一个由完全独立的代码修改的 ObservableList。我的问题是,我不仅希望在列表更改时调用 ListChangeListener(当我附加监听器时,列表可能已经包含元素),而且我还希望将
我正在尝试通过按最匹配的记录对记录进行排序来改进我的网站之一中的搜索功能。 我有以下 mysql 表。 调查回复 +--------+-------------+------------------+
我想给予 最高优先级.在我的示例中,我想要 的背景-要显示的元素,而不是为 指定的内容-元素。 h1{ background-color:blue1!important } 但它在下面的上下
我正在使用 SliverAppBar和 SliverListView在我的项目中。 我需要BorderRadius到我的 SliverList这是我的 SliverAppBar 的底部. 这是我需要的
我有它,这样当您(PaintBrush)完成时,一切都会清除并出现一个按钮。单击该按钮时,它开始二级,在这里它创建一个新的 Canvas 。我添加了一些代码,以便在单击按钮时删除旧 Canvas ,然
在下面的代码中,我分析给定的包以获取使用给定注释注释的所有类。 我想将它们及其注释(及其值)加载到 map 中。 package com.test @Named("valueToStock") pub
HTML: Div CSS: body{ width: 600px; height: 600px; background: red; }
我在我的图片库中应用了 jquery lighbox,但由于图像大小可变,灯箱大小不固定,因此以图像的原始大小打开,这反过来导致 biga 图像超出屏幕并显示浏览器中的水平滚动条。 因此,我正在寻找将
无论如何,包含文件是否可以在父范围内使用到它被调用的范围?以下示例经过简化,但完成相同的工作。 本质上,一个文件将被一个函数包含,但希望被包含文件的范围是调用包含它的函数的范围。 主.php: get
我有一个 html 页面,其中包含许多使用 a 标记的链接。我想在不同的选项卡中打开所有链接,而不是在所有标签中设置 target="_blank" ,有没有像下面的css那样做: a{target=
我正在使用 Zend_Navigation 并试图将它与 Zend_Acl 集成。导航中的每个页面都有一个 privilege 属性。我无法确定的是如何为单个页面定义多个权限。 用例:用于管理用户的页
所以,我的代码(Perl 脚本和 Perl 模块)位于这样的树中: trunk/ util/ process/ scripts/ 'util' 目录有,嗯,实用程序,'pr
这个问题在这里已经有了答案: How to clone a Date object? (8 个答案) 关闭 4 年前。 我正在处理日期,我的代码如下所示: (currentDate 只是要记住的实际
我刚开始使用 Crashlytics。我已经开始探索使用日志记录并创建了一个示例 ios 项目来测试它。我可以看到 CLSLogv 命令的第一个参数,但缺少第二个参数 例子:CLSLogv("Butt
我是 asp.net 的新手,我想为链接标签添加样式。我的代码如下: Guest .userlabel { display:inline-block; text-decoration:
我有一个页面,其中我使用 HTML 表格中的 RadiobuttonList。我已经为 table 和 td 提供了 CSS 样式。我还为 RadioButtonList 提供了 CSS 样式,但它没
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我是一名优秀的程序员,十分优秀!