2014-03-02 51 views
1

我在从GitHub获取的库中遇到问题。 图书馆导致圆形图像。圆角部分运行得很好,但图像调整大小不如其他部分大。它不同的图像和图像,我想这个工作调整任何图像的大小,我的视图在Android上的大小。例如,如果我用android:layout_width="100dp"调用这个,我想要调整图像大小。图像在Android上无法正确调整大小

非常感谢您的时间。

这是库:

package com.roundimage.support; 

import android.annotation.SuppressLint; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapShader; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Shader; 
import android.graphics.drawable.BitmapDrawable; 
import android.provider.MediaStore; 
import android.util.AttributeSet; 
import android.widget.ImageView; 

public class CircularImageView extends ImageView { 

    private int borderWidth = 3; 
    private int viewWidth; 
    private int viewHeight; 
    private Bitmap image; 
    private Paint paint; 
    private Paint paintBorder; 
    private BitmapShader shader; 

    public CircularImageView(Context context) { 
     super(context); 
     setup(); 
    } 

    public CircularImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setup(); 
    } 

    public CircularImageView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setup(); 
    } 

    private void setup() 
    { 
     // init paint 
     paint = new Paint(); 
     paint.setAntiAlias(true); 

     paintBorder = new Paint(); 
     setBorderColor(Color.WHITE); 
     paintBorder.setAntiAlias(true);  
    } 

    public void setBorderWidth(int borderWidth) 
    { 
     this.borderWidth = borderWidth; 
     this.invalidate(); 
    } 

    public void setBorderColor(int borderColor) 
    {  
     if(paintBorder != null) 
      paintBorder.setColor(borderColor); 

     this.invalidate(); 
    } 

    private void loadBitmap() 
    { 
     BitmapDrawable bitmapDrawable = (BitmapDrawable) this.getDrawable(); 

     if(bitmapDrawable != null) 
      image = bitmapDrawable.getBitmap(); 
    } 

    @SuppressLint("DrawAllocation") 
    @Override 
    public void onDraw(Canvas canvas) 
    { 
     //load the bitmap 
     loadBitmap(); 

     // init shader 
     if(image !=null) 
     {   
      shader = new BitmapShader(Bitmap.createScaledBitmap(image, canvas.getWidth(), canvas.getHeight(), false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 
      paint.setShader(shader); 
      int circleCenter = viewWidth/2; 

      // circleCenter is the x or y of the view's center 
      // radius is the radius in pixels of the cirle to be drawn 
      // paint contains the shader that will texture the shape 
      canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter + borderWidth, paintBorder); 
      canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter, paint); 
     }  
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
    { 
     int width = measureWidth(widthMeasureSpec); 
     int height = measureHeight(heightMeasureSpec, widthMeasureSpec);   

     viewWidth = width - (borderWidth *2); 
     viewHeight = height - (borderWidth*2); 

     setMeasuredDimension(width, height); 
    } 

    private int measureWidth(int measureSpec) 
    { 
      int result = 0; 
      int specMode = MeasureSpec.getMode(measureSpec); 
      int specSize = MeasureSpec.getSize(measureSpec); 

      if (specMode == MeasureSpec.EXACTLY) { 
       // We were told how big to be 
       result = specSize; 
      } else { 
       // Measure the text 
       result = viewWidth; 

      } 

     return result; 
    } 

    private int measureHeight(int measureSpecHeight, int measureSpecWidth) { 
     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 { 
      // Measure the text (beware: ascent is a negative number) 
      result = viewHeight;   
     } 
     return result; 
    } 
} 

这是我如何调用它的XML:

<com.roundimage.support.CircularImageView 
    android:id="@+id/item_pic" 
    android:layout_width="70dp" 
    android:layout_height="70dp" 
    android:src="@drawable/example_id" /> 

这怎么保持这样的: enter image description here

以及如何图像确实是: enter image description here

并针对不同的图像大小不同,对于diferent分辨率不同的设备时,图像会出现在不同的方式..

回答

0

尝试使用scaleType在XML是fitXY

机器人:ScaleType =“fitXY”

或程序

imageview.setScaleType(ScaleType.FITXY)

如果它不工作,尝试另一种scaletype。

编辑:看到这个职位here

+0

没有工作......其中任何:( – user3050910

+0

请参阅编辑... – youssefhassan

0

@youssefhassan链接指向它:

尝试在布局添加该

你必须调整您的图像所需大小:

` private void loadBitmap() { BitmapDrawable bitmapDrawable =(BitmapDrawable)this.getDrawable();

if(bitmapDrawable != null) { 
     int size = getPixelsFromDp(70); 
     Bitmap imageRealSize = bitmapDrawable.getBitmap(); 
     image = Bitmap.createScaledBitmap(imageRealSize, size, size, true); 
    } 
} 

` 其中“getPixelsFromDp”是一个函数来获取DP的你在像素上添加XML(你可以找到的页面负载,以获取其实现)

相关问题