mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Exposed properties to allow image to be cropped when there is border radius
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
package org.nativescript.widgets;
|
package org.nativescript.widgets;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.graphics.*;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -11,6 +12,11 @@ import android.graphics.drawable.Drawable;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class ImageView extends android.widget.ImageView {
|
public class ImageView extends android.widget.ImageView {
|
||||||
|
private float cornerRadius = 0;
|
||||||
|
private float borderWidth = 0;
|
||||||
|
|
||||||
|
private Path path = new Path();
|
||||||
|
private RectF rect = new RectF();
|
||||||
|
|
||||||
private double scaleW = 1;
|
private double scaleW = 1;
|
||||||
private double scaleH = 1;
|
private double scaleH = 1;
|
||||||
@@ -20,6 +26,28 @@ public class ImageView extends android.widget.ImageView {
|
|||||||
this.setScaleType(ScaleType.FIT_CENTER);
|
this.setScaleType(ScaleType.FIT_CENTER);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public float getCornerRadius() {
|
||||||
|
return this.cornerRadius;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCornerRadius(float radius) {
|
||||||
|
if (radius != this.cornerRadius) {
|
||||||
|
this.cornerRadius = radius;
|
||||||
|
this.invalidate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getBorderWidth() {
|
||||||
|
return this.borderWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBorderWidth(float radius) {
|
||||||
|
if (radius != this.borderWidth) {
|
||||||
|
this.borderWidth = radius;
|
||||||
|
this.invalidate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||||
|
|
||||||
@@ -35,8 +63,7 @@ public class ImageView extends android.widget.ImageView {
|
|||||||
if (drawable != null) {
|
if (drawable != null) {
|
||||||
measureWidth = drawable.getIntrinsicWidth();
|
measureWidth = drawable.getIntrinsicWidth();
|
||||||
measureHeight = drawable.getIntrinsicHeight();
|
measureHeight = drawable.getIntrinsicHeight();
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
measureWidth = 0;
|
measureWidth = 0;
|
||||||
measureHeight = 0;
|
measureHeight = 0;
|
||||||
}
|
}
|
||||||
@@ -81,13 +108,7 @@ public class ImageView extends android.widget.ImageView {
|
|||||||
this.setMeasuredDimension(widthSizeAndState, heightSizeAndState);
|
this.setMeasuredDimension(widthSizeAndState, heightSizeAndState);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void computeScaleFactor(
|
private void computeScaleFactor(int measureWidth, int measureHeight, boolean widthIsFinite, boolean heightIsFinite, double nativeWidth, double nativeHeight) {
|
||||||
int measureWidth,
|
|
||||||
int measureHeight,
|
|
||||||
boolean widthIsFinite,
|
|
||||||
boolean heightIsFinite,
|
|
||||||
double nativeWidth,
|
|
||||||
double nativeHeight) {
|
|
||||||
|
|
||||||
this.scaleW = 1;
|
this.scaleW = 1;
|
||||||
this.scaleH = 1;
|
this.scaleH = 1;
|
||||||
@@ -101,11 +122,9 @@ public class ImageView extends android.widget.ImageView {
|
|||||||
|
|
||||||
if (!widthIsFinite) {
|
if (!widthIsFinite) {
|
||||||
this.scaleW = scaleH;
|
this.scaleW = scaleH;
|
||||||
}
|
} else if (!heightIsFinite) {
|
||||||
else if (!heightIsFinite) {
|
|
||||||
this.scaleH = scaleW;
|
this.scaleH = scaleW;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// No infinite dimensions.
|
// No infinite dimensions.
|
||||||
switch (scale) {
|
switch (scale) {
|
||||||
case FIT_CENTER:
|
case FIT_CENTER:
|
||||||
@@ -122,4 +141,28 @@ public class ImageView extends android.widget.ImageView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onDraw(Canvas canvas) {
|
||||||
|
// floor the border width to avoid gaps between the border and the image
|
||||||
|
float roundedBorderWidth = (float) Math.floor(this.borderWidth);
|
||||||
|
float innerRadius = Math.max(0, this.cornerRadius - roundedBorderWidth);
|
||||||
|
|
||||||
|
// The border width is included in the padding so there is no need for
|
||||||
|
// clip if there is no inner border radius.
|
||||||
|
if (innerRadius != 0) {
|
||||||
|
this.rect.set(
|
||||||
|
roundedBorderWidth,
|
||||||
|
roundedBorderWidth,
|
||||||
|
this.getWidth() - roundedBorderWidth,
|
||||||
|
this.getHeight() - roundedBorderWidth);
|
||||||
|
|
||||||
|
this.path.reset();
|
||||||
|
this.path.addRoundRect(rect, innerRadius, innerRadius, android.graphics.Path.Direction.CW);
|
||||||
|
|
||||||
|
canvas.clipPath(this.path);
|
||||||
|
}
|
||||||
|
|
||||||
|
super.onDraw(canvas);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user