1、main_activity布局
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="长安图片设置桌面背景" />
<ImageView
android:id="@+id/img"
android:layout_width="110dp"
android:layout_height="154dp"
android:src="@drawable/tf_account_main_head_bg" />
</LinearLayout>
2、MainActivity
package com.example.onlogclick;
import java.io.IOException;
import javax.security.auth.PrivateCredentialPermission;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources.NotFoundException;
import android.view.Menu;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv;
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(ImageView) findViewById(R.id.img);
tv=(TextView) findViewById(R.id.tv);
//长按图片修改
img.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
try {
MainActivity.this.clearWallpaper();//清除已有的桌面
MainActivity.this.setWallpaper(MainActivity.this.img.getResources().openRawResource(R.drawable.tf_account_main_head_bg));//设置桌面图片
tv.setText("手机桌面已修改!!!");
} catch (NotFoundException e) {
tv.setText("手机桌面失败!!!");
} catch (IOException e) {
// TOD.opO Auto-generated catch block
e.printStackTrace();
}
return false;
}
});
}
}