2015-06-05 49 views
16

Tôi đang sử dụng thư viện Picasso để tải xuống hình ảnh từ URL và chuyển hình ảnh này vào chế độ xem hình tròn, nhưng vì picasso yêu cầu bạn vượt qua một imageView thực tế, tôi đã dừng lại làm thế nào để làm điều đóSử dụng thư viện picasso với hình ảnh vòng tròn

tôi đang sử dụng thư viện picasso từ đây http://square.github.io/picasso/ và xem lớp hình ảnh vòng tròn từ đây https://github.com/hdodenhof/CircleImageView

đây là sự khởi đầu của mã của tôi để có được những hình ảnh

private void getData() { 

    userName.setText(prefs.getString("userName","")); 
    jobTitle.setText(prefs.getString("profile","")); 
    userLocation.setText(prefs.getString("location","")); 




    // ??????  

    // Picasso.with(context).load(image link here).into(imageview here); 

    //CircleImageView img = new CircleImageView(this); 
    //img.setImageResource(); 
    //img.setImageBitmap(); 
    //img.setImageDrawable(); 
    //img.setImageURI(); 

} 

Edit:

đây là xml cho circleImageView

<michael.CircleImageView 
android:layout_width="100dp" 
android:layout_height="100dp" 
android:src="@drawable/shadow" 
android:layout_gravity="center" 
android:layout_marginTop="16dp" 
app:border_width="2dp" 
app:border_color="#274978" 
android:id="@+id/circleImageView" 

Trả lời

16

Sử dụng này

Hoạt động Lớp

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     String imageUrl = "https://www.baby-connect.com/images/baby2.gif"; 

     CircleImageView imageView = (CircleImageView) findViewById(R.id.image); 

     Picasso.with(getApplicationContext()).load(imageUrl) 
       .placeholder(R.drawable.images).error(R.drawable.ic_launcher) 
       .into(imageView); 
    } 
} 

Layout file

<de.hdodenhof.circleimageview.CircleImageView 
    android:id="@+id/image" 
    android:layout_width="160dp" 
    android:layout_height="160dp" 
    android:layout_centerInParent="true" 
    android:src="@drawable/images" 
    app:border_color="#ffffff" 
    app:border_width="2dp" /> 

Điều này hoạt động tốt.

24

Tôi không nghĩ rằng bạn cần CircleImageView thư viện

Bạn có thể thực hiện Thông tư Transformation kiểm tra các ý chính dưới đây

https://gist.github.com/julianshen/5829333

rồi

Picasso.with(activity).load(image link here) 
    .transform(new CircleTransform()).into(ImageView); 
+0

Cảm ơn bạn này làm việc, tuy nhiên Tôi đã chọn các mục khác như câu trả lời của tôi bởi vì nó cho phép tôi vẫn giữ biên giới xung quanh circleImageView – MichaelStoddart

+3

Không có vấn đề, bạn có thể xử lý biên giới cũng bằng cách sử dụng chuyển đổi. Vui lòng kiểm tra các nhận xét trên gist ở trên hoặc ref https://gist.github.com/berkkaraoglu/ab4caa8b1fe48231dec2 –

2

Sử dụng mã này để tạo ra Thông tư Imageview ....

public class RoundedImageView extends ImageView { 

public RoundedImageView(Context context) { 
    super(context); 
    // TODO Auto-generated constructor stub 
} 

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

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

@Override 
protected void onDraw(Canvas canvas) { 

    Drawable drawable = getDrawable(); 

    if (drawable == null) { 
     return; 
    } 

    if (getWidth() == 0 || getHeight() == 0) { 
     return; 
    } 
    Bitmap b = ((BitmapDrawable)drawable).getBitmap() ; 
    Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true); 

    int w = getWidth(), h = getHeight(); 


    Bitmap roundBitmap = getCroppedBitmap(bitmap, w); 
    canvas.drawBitmap(roundBitmap, 0,0, null); 

} 

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) { 
    Bitmap sbmp; 
    if(bmp.getWidth() != radius || bmp.getHeight() != radius) 
     sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false); 
    else 
     sbmp = bmp; 
    Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), 
      sbmp.getHeight(), Config.ARGB_8888); 
    Canvas canvas = new Canvas(output); 

    final int color = 0xffa19774; 
    final Paint paint = new Paint(); 
    final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight()); 

    paint.setAntiAlias(true); 
    paint.setFilterBitmap(true); 
    paint.setDither(true); 
    canvas.drawARGB(0, 0, 0, 0); 
    paint.setColor(Color.parseColor("#BAB399")); 
    canvas.drawCircle(sbmp.getWidth()/2+0.7f, sbmp.getHeight()/2+0.7f, 
      sbmp.getWidth()/2+0.1f, paint); 
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
    canvas.drawBitmap(sbmp, rect, rect, paint); 


      return output; 
} 

} 
1

Lấy ID của CircleImageView đầu tiên:

CircleImageView mCircleImageView = (CircleImageView)findViewById(R.id.circleImageView); 

Và vượt qua ID để Picasso thư viện:

Picasso.with(mContext).load(Uri.parse(link)).placeholder(drawable).into(mCircleImageView); 

Điều này làm việc cho tôi.

1

Tôi đã tạo ra một lớp đối tượng có sử dụng lớp RoundedBitmapDrawable mẹ đẻ của Android để làm tròn hình ảnh (loại bỏ sự cần thiết phải thêm một vòng tròn chuyển lớp để code), xem giải pháp dưới đây:

public class RoundedImageBitmapTarget implements Target { 

    private final Context context; 
    private final ImageView view; 

    public RoundedImageBitmapTarget(Context context, ImageView view) { 
     this.context = context; 
     this.view = view; 
    } 

    @Override 
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
     setBitmap(bitmap); 
    } 

    @Override 
    public void onBitmapFailed(Drawable errorDrawable) { 
     setBitmap(((BitmapDrawable) errorDrawable).getBitmap()); 
    } 

    @Override 
    public void onPrepareLoad(Drawable placeHolderDrawable) { 
     setBitmap(((BitmapDrawable) placeHolderDrawable).getBitmap()); 
    } 

    public void setBitmap(Bitmap bitmap) { 
     view.setImageDrawable(getRoundBitmap(context, bitmap)); 
    } 

    public static RoundedBitmapDrawable getRoundBitmap(Context context, Bitmap bitmap) { 
     Resources res = context.getResources(); 
     RoundedBitmapDrawable round = RoundedBitmapDrawableFactory.create(res, bitmap); 
     round.setCircular(true); 
     round.setTargetDensity(context.getResources().getDisplayMetrics()); 
     return round; 
    } 

    public static void load(Context context, ImageView view, String url, int size, @DrawableRes int placeholder) { 
     RoundedImageBitmapTarget target; 
     Picasso.with(context).load(url) 
       .resize(0, size) 
       .placeholder(placeholder) 
       .error(placeholder) 
       .into(target = new RoundedImageBitmapTarget(context, view)); 
     view.setTag(target); 
    } 

} 
Các vấn đề liên quan