انتخاب تصادفی عکس پس زمینه از گالری در اندروید استودیو
سلام
امروز میخوایم یک قطعه کد جالب در اندروید استودیو بنویسیم که به کمک اون میتونید عکس های موجود در گالری را به صورت تصادفی و خودکار در پس زمینه گوشی کاربر به نمایش بزارید، در ادامه همراه بپرسم باشید.
شروع آموزش:
ابتدا باید داده ها را از گوشی واکشی کنیم، که منظورمان همان عکس هاست که کدمون چیزی شبیه به زیر است:
۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ ۱۰ |
String[] projection = new String[]{ MediaStore.Images.Media.DATA, }; Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; Cursor cursor = managedQuery(images, projection, "", null, "" ); |
بریم سراغ کدنویسی 🙂
کد Manifest.xml:
۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ ۱۰ ۱۱ ۱۲ ۱۳ ۱۴ ۱۵ ۱۶ ۱۷ |
<?xml version="۱.۰" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.legendblogs.showbackground"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
کد Activity_main.java:
۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ ۱۰ ۱۱ ۱۲ ۱۳ ۱۴ ۱۵ ۱۶ ۱۷ ۱۸ ۱۹ ۲۰ ۲۱ ۲۲ ۲۳ ۲۴ ۲۵ ۲۶ ۲۷ ۲۸ ۲۹ ۳۰ ۳۱ ۳۲ ۳۳ ۳۴ ۳۵ ۳۶ ۳۷ ۳۸ ۳۹ ۴۰ ۴۱ ۴۲ ۴۳ ۴۴ ۴۵ ۴۶ ۴۷ ۴۸ ۴۹ ۵۰ ۵۱ ۵۲ ۵۳ ۵۴ ۵۵ ۵۶ ۵۷ ۵۸ ۵۹ ۶۰ ۶۱ ۶۲ ۶۳ ۶۴ |
ackage com.legendblogs.showbackground; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.graphics.BitmapFactory; import android.graphics.Color; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; public class MainActivity extends AppCompatActivity { Context context; private RelativeLayout main_layout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.context = this; main_layout = (RelativeLayout) findViewById(R.id.main_layout); String[] projection = new String[]{ MediaStore.Images.Media.DATA, }; Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; Cursor cursor = managedQuery(images, projection, "", null, "" ); final ArrayList<String> imagesPath = new ArrayList<String>(); if (cursor.moveToFirst()) { int dataColumn = cursor.getColumnIndex( MediaStore.Images.Media.DATA); do { imagesPath.add(cursor.getString(dataColumn)); if(imagesPath.size() > ۱۰۰) break; } while (cursor.moveToNext()); } cursor.close(); final Random random = new Random(); final int count = imagesPath.size(); handler.post(new Runnable() { @Override public void run() { int number = random.nextInt(count); String path = imagesPath.get(number); if (currentBitmap != null) currentBitmap.recycle(); currentBitmap = BitmapFactory.decodeFile(path); final int sdk = android.os.Build.VERSION.SDK_INT; if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) { BitmapDrawable ob = new BitmapDrawable(getResources(), currentBitmap); main_layout.setBackgroundDrawable(ob); } else { Drawable drawable = new BitmapDrawable(getResources(), currentBitmap); main_layout.setBackground(drawable); } handler.postDelayed(this, ۱۰۰۰); } }); } } |
کد Activity_main.xml
۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ ۱۰ ۱۱ ۱۲ ۱۳ ۱۴ ۱۵ ۱۶ ۱۷ ۱۸ ۱۹ ۲۰ |
<?xml version="۱.۰" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/main_layout" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.legendblogs.asynctask.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to Legend Blogs Exmpale" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textStyle="bold" android:textSize="۲۰dp" /> </RelativeLayout> |
کار به اتمام رسید، خروجی تهیه کنید و روی گوشی نصب کنید تا نتیجه رو مشاهده کنید 🙂
موفق باشید.
ارسال نظر
شما باید وارد شوید یا عضو شوید تا بتوانید نظر ارسال کنید