- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用以下代码在我的项目中创建圆形 ImageView 。
public class CircularImageView extends ImageView {
// Border & Selector configuration variables
private boolean hasBorder;
private boolean hasSelector;
private boolean isSelected;
private int borderWidth;
private int canvasSize;
private int selectorStrokeWidth;
// Objects used for the actual drawing
private BitmapShader shader;
private Bitmap image;
private Paint paint;
private Paint paintBorder;
private Paint paintSelectorBorder;
private ColorFilter selectorFilter;
public CircularImageView(Context context) {
this(context, null);
}
public CircularImageView(Context context, AttributeSet attrs)
{
this(context, attrs, R.attr.circularImageViewStyle);
}
public CircularImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs, defStyle);
}
/**
* Initializes paint objects and sets desired attributes.
*
* @param context
* @param attrs
* @param defStyle
*/
private void init(Context context, AttributeSet attrs, int defStyle) {
// Initialize paint objects
paint = new Paint();
paint.setAntiAlias(true);
paintBorder = new Paint();
paintBorder.setAntiAlias(true);
paintSelectorBorder = new Paint();
paintSelectorBorder.setAntiAlias(true);
// load the styled attributes and set their properties
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CircularImageView, defStyle, 0);
// Check if border and/or border is enabled
hasBorder = attributes.getBoolean(R.styleable.CircularImageView_border, false);
hasSelector = attributes.getBoolean(R.styleable.CircularImageView_selector, false);
// Set border properties if enabled
if(hasBorder) {
int defaultBorderSize = (int) (2 * context.getResources().getDisplayMetrics().density + 0.5f);
setBorderWidth(attributes.getDimensionPixelOffset(R.styleable.CircularImageView_border_width, defaultBorderSize));
setBorderColor(attributes.getColor(R.styleable.CircularImageView_border_color, Color.WHITE));
}
// Set selector properties if enabled
if(hasSelector) {
int defaultSelectorSize = (int) (2 * context.getResources().getDisplayMetrics().density + 0.5f);
setSelectorColor(attributes.getColor(
R.styleable.CircularImageView_selector_color, Color.TRANSPARENT));
setSelectorStrokeWidth(attributes.getDimensionPixelOffset(R.styleable.CircularImageView_selector_stroke_width, defaultSelectorSize));
setSelectorStrokeColor(attributes.getColor(R.styleable.CircularImageView_selector_stroke_color, Color.BLUE));
}
// Add shadow if enabled
if(attributes.getBoolean(R.styleable.CircularImageView_shadow, false))
addShadow();
// We no longer need our attributes TypedArray, give it back to cache
attributes.recycle();
}
/**
* Sets the CircularImageView's border width in pixels.
*
* @param borderWidth
*/
public void setBorderWidth(int borderWidth) {
this.borderWidth = borderWidth;
this.requestLayout();
this.invalidate();
}
/**
* Sets the CircularImageView's basic border color.
*
* @param borderColor
*/
public void setBorderColor(int borderColor) {
if (paintBorder != null)
paintBorder.setColor(borderColor);
this.invalidate();
}
/**
* Sets the color of the selector to be draw over the
* CircularImageView. Be sure to provide some opacity.
*
* @param selectorColor
*/
public void setSelectorColor(int selectorColor) {
this.selectorFilter = new PorterDuffColorFilter(selectorColor, PorterDuff.Mode.SRC_ATOP);
this.invalidate();
}
/**
* Sets the stroke width to be drawn around the CircularImageView
* during click events when the selector is enabled.
*
* @param selectorStrokeWidth
*/
public void setSelectorStrokeWidth(int selectorStrokeWidth) {
this.selectorStrokeWidth = selectorStrokeWidth;
this.requestLayout();
this.invalidate();
}
/**
* Sets the stroke color to be drawn around the CircularImageView
* during click events when the selector is enabled.
*
* @param selectorStrokeColor
*/
public void setSelectorStrokeColor(int selectorStrokeColor) {
if (paintSelectorBorder != null)
paintSelectorBorder.setColor(selectorStrokeColor);
this.invalidate();
}
/**
* Adds a dark shadow to this CircularImageView.
*/
public void addShadow() {
setLayerType(LAYER_TYPE_SOFTWARE, paintBorder);
paintBorder.setShadowLayer(4.0f, 0.0f, 2.0f, Color.BLACK);
}
@Override
public void onDraw(Canvas canvas) {
// Don't draw anything without an image
if(image == null)
return;
// Nothing to draw (Empty bounds)
if(image.getHeight() == 0 || image.getWidth() == 0)
return;
// Compare canvas sizes
int oldCanvasSize = canvasSize;
canvasSize = canvas.getWidth();
if(canvas.getHeight() < canvasSize)
canvasSize = canvas.getHeight();
// Reinitialize shader, if necessary
if(oldCanvasSize != canvasSize)
refreshBitmapShader();
// Apply shader to paint
paint.setShader(shader);
// Keep track of selectorStroke/border width
int outerWidth = 0;
// Get the exact X/Y axis of the view
int center = canvasSize / 2;
if(hasSelector && isSelected) { // Draw the selector stroke & apply the selector filter, if applicable
outerWidth = selectorStrokeWidth;
center = (canvasSize - (outerWidth * 2)) / 2;
paint.setColorFilter(selectorFilter);
canvas.drawCircle(center + outerWidth, center + outerWidth, ((canvasSize - (outerWidth * 2)) / 2) + outerWidth - 4.0f, paintSelectorBorder);
}
else if(hasBorder) { // If no selector was drawn, draw a border and clear the filter instead... if enabled
outerWidth = borderWidth;
center = (canvasSize - (outerWidth * 2)) / 2;
paint.setColorFilter(null);
canvas.drawCircle(center + outerWidth, center + outerWidth, ((canvasSize - (outerWidth * 2)) / 2) + outerWidth - 4.0f, paintBorder);
}
else // Clear the color filter if no selector nor border were drawn
paint.setColorFilter(null);
// Draw the circular image itself
canvas.drawCircle(center + outerWidth, center + outerWidth, ((canvasSize - (outerWidth * 2)) / 2) - 4.0f, paint);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
// Check for clickable state and do nothing if disabled
if(!this.isClickable()) {
this.isSelected = false;
return super.onTouchEvent(event);
}
// Set selected state based on Motion Event
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
this.isSelected = true;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_SCROLL:
case MotionEvent.ACTION_OUTSIDE:
case MotionEvent.ACTION_CANCEL:
this.isSelected = false;
break;
}
// Redraw image and return super type
this.invalidate();
return super.dispatchTouchEvent(event);
}
public void invalidate(Rect dirty) {
super.invalidate(dirty);
image = drawableToBitmap(getDrawable());
if(shader != null || canvasSize > 0)
refreshBitmapShader();
}
public void invalidate(int l, int t, int r, int b) {
super.invalidate(l, t, r, b);
image = drawableToBitmap(getDrawable());
if(shader != null || canvasSize > 0)
refreshBitmapShader();
}
@Override
public void invalidate() {
super.invalidate();
image = drawableToBitmap(getDrawable());
if(shader != null || canvasSize > 0)
refreshBitmapShader();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = measureWidth(widthMeasureSpec);
int height = measureHeight(heightMeasureSpec);
setMeasuredDimension(width, height);
}
private int measureWidth(int measureSpec) {
int result;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
// The parent has determined an exact size for the child.
result = specSize;
}
else if (specMode == MeasureSpec.AT_MOST) {
// The child can be as large as it wants up to the specified size.
result = specSize;
}
else {
// The parent has not imposed any constraint on the child.
result = canvasSize;
}
return result;
}
private int measureHeight(int measureSpecHeight) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpecHeight);
int specSize = MeasureSpec.getSize(measureSpecHeight);
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else if (specMode == MeasureSpec.AT_MOST) {
// The child can be as large as it wants up to the specified size.
result = specSize;
} else {
// Measure the text (beware: ascent is a negative number)
result = canvasSize;
}
return (result + 2);
}
/**
* Convert a drawable object into a Bitmap
*
* @param drawable
* @return
*/
public Bitmap drawableToBitmap(Drawable drawable) {
if (drawable == null) { // Don't do anything without a proper drawable
return null;
}
else if (drawable instanceof BitmapDrawable) { // Use the getBitmap() method instead if BitmapDrawable
return ((BitmapDrawable) drawable).getBitmap();
}
// Create Bitmap object out of the drawable
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
/**
* Reinitializes the shader texture used to fill in
* the Circle upon drawing.
*/
public void refreshBitmapShader() {
shader = new BitmapShader(Bitmap.createScaledBitmap(image, canvasSize, canvasSize, false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
}
/**
* Returns whether or not this view is currently
* in its selected state.
*/
public boolean isSelected() {
return this.isSelected;
}
}
圆形效果已创建,但问题是我的所有图片都变窄了,因为图片太大并且试图适应圆形 ImageView 。任何可能的解决方案?谢谢!
最佳答案
假设圆形 ImageView 的大小是固定的,避免拉伸(stretch)图像的唯一方法是将其裁剪为与 ImageView 相同的纵横比,或者在顶部或侧面引入边距。
因为您已经在 refreshBitmapShader() 中将位图缩放到 Canvas 大小,所以这是使用 Bitmap.createBitmap() 裁剪它的方便位置:
public void refreshBitmapShader() {
int left = 0; y = 0; w = image.getWidth(), h = image.getHeight();
// decide whether we have to crop the sizes or the top and bottom:
if(w > h) // width is greater than height
{
x = (w - h) >> 1; // crop sides, half on each side
w = h;
}
else
{
y = (h - w) >> 1; // crop top and bottom
h = w;
}
Matrix m = new Matrix();
float scale = (float)canvasSize / (float)w;
m.preScale(scale, scale); // scale to canvas size
shader = new BitmapShader(Bitmap.createBitmap(image, x, y, w, h, m, false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
}
关于java - 圆形 ImageView 拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29074206/
好吧,这是最好用图像来解释的事情...... 我正在寻找一个类似于 StretchBlt 的函数,但我可以将图像复制到定义目标四个角的 Canvas 上,即将图像的梯形/四边形拉伸(stretch)绘
我为 UITextField 制作了一个自定义键盘,其中包含 18 个 UIButton。它是一个 UIView,被设置为文本字段的 inputView。 按钮是在 UIView 的 initWith
我想创建一个 UIButton,它使用可拉伸(stretch)图像作为背景图像,例如我可以轻松调整按钮大小以适应不同的标签等。 所以我创建了以下运行良好的代码: UIImage *bgImage =
我正在尝试拉伸(stretch) UIImageView 中的图像 - 但我惨败了:) 以下设置: 带有 View 和附加到该 View 的 UIImageView 的 NIB 文件。 使用IBOut
我一直在尝试使用 html Canvas ,但由于某种原因,很难让它不拉伸(stretch)东西。我试过只使用 screen.width 和 screen.height,但我通过使用 screen.a
我想拉伸(stretch)上图的左右两侧,并保持中间的箭头不拉伸(stretch)。我怎样才能做到这一点? 最佳答案 如果你不喜欢打扰 Photoshop 并且图像不需要动态调整大小,你可以使用我在
我的页脚在一个容器中有四列。它需要在容器内以与上面的内容对齐。 我的问题是我想让左栏有红色背景,但目前它不会拉伸(stretch),因为它显然在容器中。 我怎样才能将它向左拉伸(stretch)整个宽
有没有办法拉伸(stretch)按钮,使中央部分保持完整? 9-patch 不允许这样做。 编辑:我按照 Benito 的建议使用了 9-patch。它工作正常。奇怪的是,我第一次无法得到它。 最佳答
我想拉伸(stretch)上图的左右两边,中间的箭头不拉伸(stretch)。我该怎么做? 最佳答案 如果你不想打扰 Photoshop 并且如果图像不需要动态调整大小,你可以在 UIImage 上使
我创建了一个 9patch 图像,不知何故它只能垂直拉伸(stretch)。我尝试了其他 9-patch 图像,但它们具有相同的效果,为什么它们在其他情况下工作。所以我认为 9patch 应该没问题。
我需要在边框控件(或类似控件)中绘制一些简单的线条,这些线条始终延伸到边框的边界。有没有办法只拉伸(stretch)线条而不是它的笔?不涉及大量 C#? 在这个版本中,线条延伸:
这是我的 XAML 及其外观:
编辑:答案将允许背景图像根据 body 的大小改变它的高度。如果正文是 500px 高,它应该是 100% 宽度,500px 高度。或 100% 宽度 2500 像素高度。 也许我错过了这件事,但我正
我有一个 UILabel。我设置了尾随和前导空间约束,还添加了对齐 Y 中心约束。我已将行数设置为零。 我的目标是将 UILabel 拉伸(stretch)到某个最大宽度,然后添加新行。在给定的约束条
我正在制作一个网站,对于该网站,我正在使用顶部的导航栏。我想要的是将栏放在页面的顶部,并且它是主页的单独颜色。这是我当前的代码: body { font-family: "Baloo Bhaina
我编写了代码来 reshape 已纹理化的四边形。当我拉伸(stretch)四边形时,图像确实按照我的预期拉伸(stretch)了。我似乎在拉伸(stretch)图像时有两个不同的三角形,图像被拉伸(
我在 div 中嵌入了 YouTube。此 div .entry-content 的 max-width 为 30em。 YouTube 嵌入我想要全窗口宽度,延伸到 #container 之外。为此
看: //UIImageView* stretchTest = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]
我有一个 super 简单的网页,由纯 HTML/CSS 组成。正如预期的那样,当我在本地运行它时,一切正常。 但是,当我部署它时(使用 Droppages,一个通过 Dropbox 的静态页面托管平
我正在尝试将一个 9 色 block 图像设置为我的一项 Activity 的背景,但它不必要地拉伸(stretch),使一切变得奇怪。我使用 draw9patch 创建它,并在左下角添加了一个像素,
我是一名优秀的程序员,十分优秀!