2017-06-10 4 views
-1

ich aus dem externen Speicher erhalten Bild und legen Sie das Bild in der runden Form Und sparen auch das Bild in sheredpreferncerunde Form Bild ohne andere Bibliothek

I may Art und Weise versucht, aber ich kann es nicht

machen Ich brauche es ohne externe Bibliothek

Antwort

0

Für abgerundete Bildansicht ohne Bibliothek können Sie die folgende Klasse verwenden.

package com.sample; 

import android.content.Context; 
import android.content.res.Resources; 
import android.content.res.TypedArray; 
import android.graphics.Bitmap; 
import android.graphics.BitmapShader; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Shader; 
import android.graphics.drawable.BitmapDrawable; 
import android.util.AttributeSet; 
import android.widget.ImageView; 

/** 
* Custom Circular Image view. 
*/ 
public class RoundImageview extends ImageView { 

    private int borderWidth = 0; 
    private int viewWidth; 
    private int viewHeight; 
    private Bitmap image; 
    private Paint paint; 
    private Paint paintBorder; 
    private Paint shaderPaint; 
    private BitmapShader shader; 
    private static int mBorderColor = android.R.color.white; 
    private Context context; 

    private boolean needToDrawOverlay = true; 
    private AttributeSet attrs; 

    public RoundImageview(final Context context) { 
     super(context); 
     this.context = context; 
    } 

    public RoundImageview(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.context = context; 
     this.attrs = attrs; 
     init(); 
    } 

    public RoundImageview(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     this.context = context; 
     this.attrs = attrs; 
     init(); 
    } 

    /** 
    * To initialize the paint components. 
    */ 
    private void init() { 
     Resources res = context.getResources(); 
     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView, 0, 0); 
     int backgroundColor = a.getColor(R.styleable.RoundImageView_bg_color, 
       res.getColor(android.R.color.darker_gray)); 
     a.recycle(); 

     paint = new Paint(); 
     paint.setAntiAlias(true); 

     shaderPaint = new Paint(); 
     shaderPaint.setAntiAlias(true); 
     shaderPaint.setColor(backgroundColor); 
     shaderPaint.setStyle(Paint.Style.FILL); 

     paintBorder = new Paint(); 
     paintBorder.setColor(mBorderColor); 
     borderWidth = 2; 
     paintBorder.setAntiAlias(true); 

    } 

    @Override 
    public void onDraw(Canvas canvas) { 
     // load the bitmap 
     BitmapDrawable bitmapDrawable = (BitmapDrawable) this.getDrawable(); 
     if (bitmapDrawable != null) { 
      image = bitmapDrawable.getBitmap(); 
     } 

     if (needToDrawOverlay) { 
      int circleCenter = viewWidth/2; 

      canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter 
        + borderWidth - 4.0f, shaderPaint); 
     } 

     // init shader 
     if (image != null) { 
      shader = createBitmapShader(image, canvas); 

      paint.setShader(shader); 

      // calculate the center point. 
      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 - 4.0f, paintBorder); 
      canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, 
        circleCenter - 4.0f, paint); 
     } 

    } 

    /** 
    * To create bitmap shader. 
    * 
    * @param image 
    * @param canvas 
    * @return BitmapShader object 
    */ 
    private BitmapShader createBitmapShader(Bitmap image, Canvas canvas) { 
     return new BitmapShader(Bitmap.createScaledBitmap(image, this.getWidth(), this.getHeight(), 
       false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 
    } 

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

     viewWidth = width; 
     viewHeight = height; 

     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); 
    } 

} 

ein attrs.xml im Werte Modul des Moduls res erstellen

<!-- Round Image view --> 
<declare-styleable name="RoundImageView"> 
    <attr name="bg_color" format="color" /> 
    <attr name="border_color" format="color" /> 
</declare-styleable> 

es in Ihrem Layout verwenden diese Weise

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:roundImageview="http://schemas.android.com/apk/res-auto" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <com.sample.RoundImageview 
     android:id="@+id/imageButton" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_centerInParent="true" 
     android:src="@drawable/image" 
     roundImageview:bg_color="#000000" /> 

</RelativeLayout> 
+0

zu Bild in geteilt speichern Einstellungen können Sie diese https://stackoverflow.com/questions/8586242/how-can-i-store-images-using-sharedpreference-in-android verweisen –

Verwandte Themen