Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Android में कलर इंटीजर को हेक्स स्ट्रिंग में कैसे बदलें?

Android में रंग को इंटीजर (ARGB) से हेक्साडेसिमल स्ट्रिंग (#RRGGBB या #AARRGGBB) में बदलने का तरीका।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में नया प्रोजेक्ट बनाएं।

चरण 2: लेआउट

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"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="24sp"
        android:textStyle="bold" />
</RelativeLayout>

चरण 3: रूपांतरण लॉजिक

src/MainActivity.java:

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

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

        int intColor = -16895234; // उदाहरण: नीला रंग
        String hexColor = Integer.toHexString(intColor);

        // ARGB फॉर्मेट में 8 हेक्स डिजिट होते हैं
        // पहला 2 डिजिट अल्फा (FF = अपारदर्शी)
        // substring(2) से अल्फा हटाकर #RRGGBB प्राप्त करें
        String hexWithoutAlpha = hexColor.substring(2).toUpperCase();
        textView.setText("#" + hexWithoutAlpha);
    }
}

रंग इंटीजर फॉर्मेट

Android में रंग 32-बिट इंटीजर होता है: 0xAARRGGBB

  • AA - अल्फा (00=पारदर्शी, FF=अपारदर्शी)
  • RR - लाल
  • GG - हरा
  • BB - नीला

उपयोगी हेल्पर मेथड्स

// इंटीजर से #RRGGBB (अल्फा के बिना)
public static String toHexNoAlpha(int color) {
    return String.format("#%06X", (0xFFFFFF & color));
}

// इंटीजर से #AARRGGBB (अल्फा के साथ)
public static String toHexWithAlpha(int color) {
    return String.format("#%08X", color);
}

// हेक्स स्ट्रिंग से इंटीजर
public static int fromHex(String hex) {
    return Color.parseColor(hex);
}

// Color क्लास उपयोग (API 1+)
int color = Color.rgb(255, 0, 0); // लाल
int colorWithAlpha = Color.argb(255, 255, 0, 0); // अपारदर्शी लाल

// कंपोनेंट निकालें
int red = Color.red(colorInt);
int green = Color.green(colorInt);
int blue = Color.blue(colorInt);
int alpha = Color.alpha(colorInt);

उदाहरण आउटपुट

  • Color.RED (-65536) → #FF0000
  • Color.BLUE (-16776961) → #0000FF
  • Color.WHITE (-1) → #FFFFFF
  • Color.TRANSPARENT (0) → #00000000

चरण 4: मैनिफेस्ट

मानक मैनिफेस्ट।

  1. एंड्रॉइड में इमेज को Base64 स्ट्रिंग में कैसे बदलें? एंड्रॉइड में इमेज को Base64 स्ट्रिंग में कैसे बदलें?

    Bitmap को Base64 एन्कोडेड स्ट्रिंग में कनवर्ट करें यह उदाहरण दर्शाता है कि ड्रॉएबल रिसोर्स से Bitmap लोड करके Base64 स्ट्रिंग में कैसे बदलें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: साधारण लेआउट res/layout/activity_main.xml: <?xml version=1.0 encoding=utf-

  1. एंड्रॉइड में TextView स्पैन का कलर कैसे सेट करें? एंड्रॉइड में TextView स्पैन का कलर कैसे सेट करें?

    SpannableString और ForegroundColorSpan के साथ आंशिक टेक्स्ट कलरिंग यह उदाहरण दर्शाता है कि SpannableString का उपयोग करके TextView के विशिष्ट भागों का टेक्स्ट कलर कैसे बदलें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: लेआउट में TextView res/layout/activity_main.x

  1. Android में मिलीसेकंड को डेट फॉर्मेट में कैसे बदलें? Android में मिलीसेकंड को डेट फॉर्मेट में कैसे बदलें?

    Android में मिलीसेकंड से डेट कन्वर्जन गाइड यह ट्यूटोरियल दिखाता है कि कैसे epoch मिलीसेकंड को पढ़ने योग्य डेट फॉर्मेट में बदला जाता है, SimpleDateFormat और आधुनिक java.time API का उपयोग करके। चरण 1: प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: लेआउट फाइल res/layout/activity_main.