एंड्रॉइड में बिटमैप को गोलाकार क्रॉप करना (बॉर्डर और शैडो के साथ)
यह उदाहरण दिखाता है कि Canvas, Paint, और PorterDuffXfermode का उपयोग करके बिटमैप से गोलाकार क्षेत्र कैसे निकाला जाता है, साथ ही बॉर्डर और शैडो भी जोड़े जाते हैं।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट XML
res/layout/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rl"
android:padding="16dp"
android:background="#edf2ea"
tools:context=".MainActivity">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="गोलाकार इमेज" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="गोलाकार बनाएं" />
</RelativeLayout>
चरण 3: MainActivity में गोलाकार क्रॉप लॉजिक
MainActivity.java:
package com.medkart.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
private Context mContext;
private Resources mResources;
private RelativeLayout mRelativeLayout;
private Button mBtn;
private ImageView mImageView;
private Bitmap mBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = getApplicationContext();
mResources = getResources();
mRelativeLayout = findViewById(R.id.rl);
mImageView = findViewById(R.id.iv);
mBtn = findViewById(R.id.btn);
// मूल इमेज सेट करें
final int bitmapResId = R.drawable.flower; // अपनी इमेज का नाम डालें
mImageView.setImageBitmap(BitmapFactory.decodeResource(mResources, bitmapResId));
mBtn.setOnClickListener(v -> applyCircularCrop(bitmapResId));
}
private void applyCircularCrop(int bitmapResId) {
// 1. मूल बिटमैप लोड करें
mBitmap = BitmapFactory.decodeResource(mResources, bitmapResId);
// 2. गोलाकार क्रॉप करें
mBitmap = getCircularBitmap(mBitmap);
// 3. बॉर्डर जोड़ें
mBitmap = addBorderToCircularBitmap(mBitmap, 15, Color.WHITE);
// 4. शैडो जोड़ें
mBitmap = addShadowToCircularBitmap(mBitmap, 4, Color.LTGRAY);
// 5. ImageView में सेट करें
mImageView.setImageBitmap(mBitmap);
}
// गोलाकार बिटमैप बनाएं
private Bitmap getCircularBitmap(Bitmap srcBitmap) {
int size = Math.min(srcBitmap.getWidth(), srcBitmap.getHeight());
Bitmap dstBitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(dstBitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
Rect rect = new Rect(0, 0, size, size);
RectF rectF = new RectF(rect);
// गोलाकार पथ बनाएं
canvas.drawOval(rectF, paint);
// SRC_IN मोड: केवल ओवरलैपिंग क्षेत्र रखें
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
// स्रोत बिटमैप को केंद्र में ड्रॉ करें
float left = (size - srcBitmap.getWidth()) / 2f;
float top = (size - srcBitmap.getHeight()) / 2f;
canvas.drawBitmap(srcBitmap, left, top, paint);
srcBitmap.recycle();
return dstBitmap;
}
// बॉर्डर जोड़ें
private Bitmap addBorderToCircularBitmap(Bitmap srcBitmap, int borderWidth, int borderColor) {
int dstSize = srcBitmap.getWidth() + borderWidth * 2;
Bitmap dstBitmap = Bitmap.createBitmap(dstSize, dstSize, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(dstBitmap);
// मूल इमेज ड्रॉ करें (ऑफसेट के साथ)
canvas.drawBitmap(srcBitmap, borderWidth, borderWidth, null);
// बॉर्डर ड्रॉ करें
Paint paint = new Paint();
paint.setColor(borderColor);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(borderWidth);
paint.setAntiAlias(true);
float center = dstSize / 2f;
float radius = center - borderWidth / 2f;
canvas.drawCircle(center, center, radius, paint);
srcBitmap.recycle();
return dstBitmap;
}
// शैडो जोड़ें
private Bitmap addShadowToCircularBitmap(Bitmap srcBitmap, int shadowWidth, int shadowColor) {
int dstSize = srcBitmap.getWidth() + shadowWidth * 2;
Bitmap dstBitmap = Bitmap.createBitmap(dstSize, dstSize, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(dstBitmap);
canvas.drawBitmap(srcBitmap, shadowWidth, shadowWidth, null);
Paint paint = new Paint();
paint.setColor(shadowColor);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(shadowWidth);
paint.setAntiAlias(true);
float center = dstSize / 2f;
float radius = center - shadowWidth / 2f;
canvas.drawCircle(center, center, radius, paint);
srcBitmap.recycle();
return dstBitmap;
}
}
चरण 4: Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.medkart.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
🎯 सरल विकल्प: RoundedImageView लाइब्रेरी
उत्पादन ऐप्स के लिए, सिद्ध लाइब्रेरी का उपयोग करें:
// build.gradle implementation 'com.makeramen:roundedimageview:2.3.0'
<com.makeramen.roundedimageview.RoundedImageView
android:id="@+id/roundedImageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/your_image"
app:riv_corner_radius="50dp"
app:riv_border_width="2dp"
app:riv_border_color="#FFFFFF" />
🔑 मुख्य कॉन्सेप्ट
- PorterDuff.Mode.SRC_IN - केवल स्रोत और गंतव्य के ओवरलैपिंग भाग को रखता है
- Paint.setAntiAlias(true) - स्मूथ किनारों के लिए एंटी-अलियासिंग सक्षम करता है
- Bitmap.recycle() - उपयोग न होने वाले बिटमैप की मेमोरी मुक्त करता है
- Canvas.drawOval()/drawCircle() - गोलाकार क्लिपिंग पथ बनाता है
ऐप चलाएं
डिवाइस कनेक्ट करें, Run पर क्लिक करें। "गोलाकार बनाएं" बटन दबाने पर इमेज गोलाकार क्रॉप होकर बॉर्डर और शैडो के साथ दिखाई देगी।