Circle Image - Tạo ảnh Bo Tròn Trong Android Studio - Android

Chuyển đến nội dung chính

Android - Circle Image - Tạo ảnh bo tròn trong Android Studio

Để tạo ảnh bo tròn, đầu tiên cần có dữ liệu ảnh đầu vào dạng bitmap, sau đó thực hiện hàm sau ảnh sẽ được xử lý và trả về một bitmap: public Bitmap drawCircleImage(Bitmap imageOriginal, int radiusDp, Resources resources) { if (imageOriginal == null) { return BitmapFactory.decodeResource(resources, R.mipmap.place_hole);} int radiusPx = convertDpToPx(radiusDp, resources); Bitmap bitmapResource = Bitmap.createBitmap(radiusPx * 2, radiusPx * 2, Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(bitmapResource); Paint color = new Paint();color.setAntiAlias(true); canvas.drawCircle(radiusPx, radiusPx, radiusPx, color); // important here for circle image color.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(Bitmap.createScaledBitmap(imageOriginal, radiusPx * 2, radiusPx * 2, true), 0, 0, color); return bitmapResource;} Tránh hiện tượng ảnh tạo ra có kích thước khác nhau trên các thiết bị có độ phân giải khác nhau, sử dụng thêm hàm chuyển đổi giá trị px và dp: private int convertDpToPx(float dp, Resources resources) { float density = resources.getDisplayMetrics().density; int px = (int) (dp * density); return px;} Khi truyền vào giá trị bán kính của ảnh, ta chỉ cần quan tâm đến giá trị với đơn vị dp

Nhận xét

Đăng nhận xét

Bài đăng phổ biến từ blog này

Android - Make firework animation - Library

Step 1: Add this in build.gradle --> dependencies: compile 'com.plattysoft.leonids:LeonidsLib:1.3.2' Step 2: Use in Activity: private void fireworkAnimation(View v){ new ParticleSystem(this, 30, R.mipmap.your_image, 1500) .setSpeedRange(0.1f, 0.25f).setFadeOut(300) .setScaleRange(0,1) .setStartTime(0) .oneShot(v, 30); } Have fun! More: https://github.com/plattysoft/Leonids Đọc thêm

Android - Image Crop - Crop ảnh trong Android Studio

1. Sử dụng thư viện ngoài, cần thêm dependency sau trong build.gradle compile 'com.isseiaoki:simplecropview:1.1.4' 2. Trong xml file thêm thẻ sau <com.isseiaoki.simplecropview.CropImageView xmlns: custom = "http://schemas.android.com/apk/res-auto" android :id= "@+id/cropImageView" android :layout_width= "match_parent" android :layout_height= "match_parent" custom :scv_crop_mode= "square" custom :scv_frame_color= "@color/colorAccent" custom :scv_frame_stroke_weight= "1dp" custom :scv_guide_color= "@color/colorAccent" custom :scv_guide_show_mode= "show_on_touch" custom :scv_guide_stroke_weight= "1dp" custom :scv_handle_color= "@color/colorAccent" custom :scv_handle_show_mode= "show_always" custom :scv_handle_size= "12dp" custom :scv_min_frame_size= "@dimen/_100sdp"... Đọc thêm

Blog Archive

  • 2017 35
    • tháng 6 4
    • tháng 5 1
    • tháng 4 9
    • tháng 3 21
      • Android - Paint Simple - Tạo View vẽ hình đơn giản...
      • Android - Convert View to Image - Chụp ảnh một Vie...
      • Android - Compare Images - So sánh, tìm điểm khác ...
      • Android - Resolution - Lấy độ phân giải màn hình t...
      • Android - Shortcut Key - Phím tắt trên Android Studio
      • Android - Network Utils - Kiểm tra kết nối interne...
      • Android - Date Time - Lấy giá trị ngày, giờ theo đ...
      • Android - Circle Image - Tạo ảnh bo tròn trong And...
      • Android - Simple RecyclerView, SwipeRefreshLayout ...
      • Android - Custom SearchView - Tạo khung tìm kiếm đ...
      • Android - Image - Take Photo, Choose Photo from Ga...
      • Android - Image Crop - Crop ảnh trong Android Studio
      • Android UI - EditText - Làm việc với EditText tron...
      • Android - Spash Screen - Cách tạo màn hình bắt đầu...
      • Android - Custom Shape, Circle , Triangle with Dra...
      • Android - SeekBar - Sử dụng và tùy biến SeekBar tr...
      • Android - Audio Volume - Thay đổi âm lượng trong A...
      • Android - AsyncHttpClient - Sử dụng thư viện Async...
      • Android - Retrofit - Networking - Parse Data from ...
      • Android - Fragment - Tìm hiểu về Fragment trong An...
      • Android - Multiple Screens - Differrent Screen Siz...

Nhãn

  • Android
  • Android Studio
  • API
  • AsyncHttpClient
  • check 3g
  • check intenet connection
  • check wifi
  • code
  • develope
  • internet
  • lập trình
  • Network
  • Networking
  • Parse Json
  • Retrofit
  • stackoverflow
  • sử dụng
  • utils
Hiện thêm

Báo cáo vi phạm

Từ khóa » Bo Tròn ảnh Trong Android Studio