android-background

How can I convert String to Drawable

孤街醉人 提交于 2019-11-27 01:59:18
问题 I have many icon in drawable folder and I have their name as String. How can I access to drawable folder and change background imageView (or any view) use these name in dynamically. Thanks 回答1: This can be done using reflection: String name = "your_drawable"; final Field field = R.drawable.getField(name); int id = field.getInt(null); Drawable drawable = getResources().getDrawable(id); Or using Resources.getIdentifier() : String name = "your_drawable"; int id = getResources().getIdentifier

Scale image keeping its aspect ratio in background drawable

杀马特。学长 韩版系。学妹 提交于 2019-11-27 00:40:22
问题 How do I make a background image fit the view but keep its aspect ratio when using <bitmap /> as a background drawable XML? None of <bitmap> 's android:gravity values gives the desired effect. 回答1: It is impossible to achieve manipulating background attribute within xml-files only. There are two options: You cut/scale the bitmap programmatically with Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter) and set it as some View 's background. You use ImageView

Determining the current foreground application from a background task or service

假装没事ソ 提交于 2019-11-25 23:58:33
问题 I wish to have one application that runs in the background, which knows when any of the built-in applications (messaging, contacts, etc) is running. So my questions are: How I should run my application in the background. How my background application can know what the application currently running in the foreground is. Responses from folks with experience would be greatly appreciated. 回答1: With regards to "2. How my background application can know what the application currently running in the