android壁纸背景,android – 选择动态壁纸的背景

我这样做是通过将一个首选项放入Settings xml中(我的是flash_setting.xml);

android:key="image_custom"

android:title="Choose Background"

android:summary="Select a Custom Image"

/>

我创建了一个自定义类来获取OnPreferenceClick侦听器并监视用户单击首选项(这是调用mySettings.java)(请注意,getRealPathFromURI例程不是我的,并且在此处的其他位置找到);

您的类应首先扩展PreferenceActivity并实现Sharedpreference更改侦听器

public class flashSettings extends PreferenceActivityimplements SharedPreferences.OnSharedPreferenceChangeListener {

链接到首选项名称并注册侦听器

@Override

protected void onCreate(Bundle icicle) {

super.onCreate(icicle);

getPreferenceManager().setSharedPreferencesName(

fingerflashpro.SHARED_PREFS_NAME);

addPreferencesFromResource(R.xml.flash_settings); getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(

this);

接下来,我们将设置on首选项侦听器以侦听“image_custom”.单击它时,我们将启动一个新的意图来显示照片选择器.我们从StartActivityForResult开始,这样我们就可以从intent中获取图像的URI.

getPreferenceManager().findPreference("image_custom").setOnPreferenceClickListener(new OnPreferenceClickListener()

{

@Override

public boolean onPreferenceClick(Preference preference)

{

Display display = getWindowManager().getDefaultDisplay();

int width = display.getWidth();

int height = display.getHeight();

Toast.makeText(getBaseContext(), "Select Image - " + (width) + " x " + height , Toast.LENGTH_LONG).show();

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);

photoPickerIntent.setType("image/*");

startActivityForResult(photoPickerIntent, 1);

return true;

}

});}

接下来,我们等待活动返回结果,并将URI解析为实际路径.

@Override

public void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 1) {

if (resultCode == Activity.RESULT_OK) {

Uri selectedImage = data.getData();

String RealPath;

SharedPreferences customSharedPreference = getSharedPreferences(fingerflashpro.SHARED_PREFS_NAME, Context.MODE_PRIVATE);

SharedPreferences.Editor editor = customSharedPreference.edit ();

RealPath = getRealPathFromURI (selectedImage);

editor.putString("image_custom", RealPath);

editor.commit();

}}

在这个网站上找到了以下代码(由PercyPercy在this thread上发现),我只是为了完整性而包含它.然而,它确实完美.

public String getRealPathFromURI(Uri contentUri) {

String [] proj={MediaColumns.DATA};

Cursor cursor = managedQuery( contentUri,

proj, // Which columns to return

null, // WHERE clause; which rows to return (all rows)

null, // WHERE clause selection arguments (none)

null); // Order-by clause (ascending by name)

int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);

cursor.moveToFirst();

return cursor.getString(column_index);}

确保我们实现所需的覆盖;

@Override

protected void onResume() {

super.onResume();

}

@Override

protected void onDestroy() {

getPreferenceManager().getSharedPreferences().

unregisterOnSharedPreferenceChangeListener(this);

super.onDestroy();

}

@Override

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,

String key) {

}}

然后在您的主要壁纸服务活动中,您可以从共享首选项中提取图像的路径.

@Override

public void onSharedPreferenceChanged(SharedPreferences prefs,

String key) {

imageBg = prefs.getString("image_custom", "Bad Image");

getBackground();}

这是加载图像的相当粗略的例程.我试图引入一些错误捕获,以防文件被删除,重命名或SD卡被挂载(因此你丢失了你的图像).我还试图对设备方向进行一些粗略的检查.我相信你能做得更好.

还有一些Samplesize检查,因此您不会超过VM预算.这是此代码中防止强制关闭的最重要部分,应该包含在内.

当手机的方向改变时,我也会调用此例程,以便每次调整背景的大小.

void getBackground() {

if (this.cvwidth == 0 || this.cvheight == 0 || this.visibleWidth == 0) {

this.cvwidth = 480;

this.cvheight = 854;

this.visibleWidth = 480;}

if(new File(imageBg).exists()) {

int SampleSize = 1;

do {

BitmapFactory.Options options = new BitmapFactory.Options();

options.inJustDecodeBounds = true;

bg = BitmapFactory.decodeFile(imageBg, options);

SampleSize = (int) (Math.ceil(options.outWidth/(this.visibleWidth * 2))*2);

options.inJustDecodeBounds = false;

try {options.inSampleSize = SampleSize;

bg = BitmapFactory.decodeFile(imageBg, options);}

catch (OutOfMemoryError e) {

SampleSize = SampleSize * 2;

}

} while (bg == null);

bg = Bitmap.createScaledBitmap(bg, this.cvwidth/2, this.cvheight, true);}

else {bg = BitmapFactory.decodeResource(getResources(), R.drawable.bg);

bg = Bitmap.createScaledBitmap(bg, this.cvwidth/2, this.cvheight, true);}

LoadText = "";

}

我希望这有帮助.我花了一个绝对的年龄来提出这一切,我知道还有一些方面我可以改进,但至少它应该让你去.

如果有人有关于使这个代码更好的建议,那么我全都耳朵.

要在Android中创建自定义四边形的形状背景,可以使用ShapeDrawable类。以下是创建平行四边形形状背景的步骤: 1. 在drawable文件夹中创建一个xml文件,例如parallelogram.xml。 2. 在xml文件中定义ShapeDrawable类。 3. 在ShapeDrawable类中设置形状的类型为矩形,并设置矩形的大小和颜色。 4. 使用Path类创建一个路径,该路径将定义平行四边形的形状。 5. 使用PathMeasure类测量路径的长度,并计算出路径的四个端点。 6. 将四个端点添加到平行四边形的路径中。 7. 将路径设置为ShapeDrawable类的形状。 8. 将xml文件应用为视图的背景。 以下是一个示例的parallelogram.xml文件: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#FF0000" /> <padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp" /> <size android:width="120dp" android:height="80dp" /> <corners android:radius="5dp" /> <stroke android:width="2dp" android:color="#000000" /> <padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp" /> <solid android:color="#FF0000" /> <padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp" /> <size android:width="120dp" android:height="80dp" /> <corners android:radius="5dp" /> <stroke android:width="2dp" android:color="#000000" /> <padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp" /> <solid android:color="#FF0000" /> <padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp" /> <size android:width="120dp" android:height="80dp" /> <corners android:radius="5dp" /> <stroke android:width="2dp" android:color="#000000" /> <padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp" /> <solid android:color="#FF0000" /> <padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp" /> <size android:width="120dp" android:height="80dp" /> <corners android:radius="5dp" /> <stroke android:width="2dp" android:color="#000000" /> <padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp" /> <solid android:color="#FF0000" /> <padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp" /> <size android:width="120dp" android:height="80dp" /> <corners android:radius="5dp" /> <stroke android:width="2dp" android:color="#000000" /> <padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp" /> <solid android:color="#FF0000" /> <padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp" /> <size android:width="120dp" android:height="80dp" /> <corners android:radius="5dp" /> <stroke android:width="2dp" android:color="#000000" /> <padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp" /> <solid android:color="#FF0000" /> <padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp" /> <size android:width="120dp" android:height="80dp" /> <corners android:radius="5dp" /> <stroke android:width="2dp" android:color="#000000" /> <padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp" /> <solid android:color="#FF0000" /> <padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp" /> <size android:width="120dp" android:height="80dp" /> <corners android:radius="5dp" /> <stroke android:width="2dp" android:color="#000000" /> </shape> ``` 可以在视图中使用以下代码应用此背景: ``` <View android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/parallelogram" /> ``` 这将创建一个大小为120dp x 80dp的红色矩形,具有黑色边框和圆角,形状为平行四边形。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值