在AndroidApp應用中,OnLongClick事件表示長按2秒以上觸發的事件,本章我們通過長按圖像設置為墻紙來理解其具體用法。
知識點:OnLongClickListener
OnLongClickListener接口與之前介紹的OnClickListener接口原理基本相同,只是該接口為View長按事件的捕捉接口,即當長時間按下某個View時觸發的事件,該接口對應的回調方法簽名如下。
public boolean onLongClick(View v)
參數v:參數v為事件源控件,當長時間按下此控件時才會觸發該方法。
返回值:該方法的返回值為一個boolean類型的變量,當返回true時,表示已經完整地處理了這個事件,并不希望其他的回調方法再次進行處理;當返回false時,表示并沒有完全處理完該事件,更希望其他方法繼續對其進行處理。
一、設計界面
1、首先把a.jpg、b.jpg、c.jpg、d.jpg、e.jpg、prov.png、next.png圖片復制到res/drawable-hdpi文件夾內。
2、打開“res/layout/activity_main.xml”文件,生成ImageButton按鈕。
?。?)從工具欄向activity拖出1個圖像ImageView、2個圖像按鈕ImageButton。該控件來自Image&Media。
3、打開activity_main.xml文件。
我們把自動生成的代碼修改成如下代碼,具體為:
(1)ImageView的id修改為picture;
?。?)“上一幅”按鈕ImageButton的id修改為prov;
?。?)設置android:padding="0dp",按鈕灰色邊框去掉。
?。?)“下一幅”按鈕ImageButton的id修改為next;
?。?)設置android:padding="0dp",按鈕灰色邊框去掉。
代碼如下:
<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: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=".MainActivity" >
<ImageButton
android:id="@+id/prov"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="99dp"
android:layout_marginLeft="28dp"
android:src="@drawable/prov"
android:padding="0dp"/>
<ImageButton
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/prov"
android:layout_marginLeft="79dp"
android:layout_toRightOf="@+id/prov"
android:src="@drawable/next"
android:padding="0dp"/>
<ImageView
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/prov"
android:layout_centerHorizontal="true"
android:layout_marginBottom="101dp"
android:src="@drawable/a" />
</RelativeLayout>
二、長按事件
打開“src/com.genwoxue.onlongclick/MainActivity.java”文件。
然后輸入以下代碼:
package com.example.hw;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView ivwPicture = null;
private ImageButton ibtnProv = null;
private ImageButton ibtnNext = null;
//聲明5個圖像
private Integer[] iImages = {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivwPicture = (ImageView) super.findViewById(R.id.picture);
ibtnProv = (ImageButton) findViewById(R.id.prov);
ibtnNext = (ImageButton) findViewById(R.id.next);
//注冊OnClick監聽器
ibtnProv.setOnClickListener(new OnClickListener(){
private int i = 5;
public void onClick(View v) {
if(i>0){
ivwPicture.setImageResource(iImages[--i]);
}else if(i==0){
i = 4;
ivwPicture.setImageResource(iImages[4]);
}
}
});
//注冊OnClick監聽器
ibtnNext.setOnClickListener(new OnClickListener(){
private int i = 0;
public void onClick(View v) {
if(i<5){
ivwPicture.setImageResource(iImages[i++]);
}else if(i==5){
i = 1;
ivwPicture.setImageResource(iImages[0]);
}
}
});
//注冊OnlongClick監聽器
ivwPicture.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
try{
//清空當前墻紙
MainActivity.this.clearWallpaper();
//當前view轉換為ImageView對象
ImageView iv = (ImageView) view;
//啟用圖形緩沖
iv.setDrawingCacheEnabled(true);
//使用當前緩沖圖形創建Bitmap
Bitmap bmp = Bitmap.createBitmap(iv.getDrawingCache());
//當前圖片設置為墻紙
MainActivity.this.setWallpaper(bmp);
//清理圖形緩沖
iv.setDrawingCacheEnabled(false);
Toast.makeText(getApplicationContext(), "背景設置陳功", Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(getApplicationContext(), "背景設置失敗", Toast.LENGTH_LONG).show();
}
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
通過“上一幅”、“下一幅”按鈕瀏覽圖片,長按圖片可以把當前圖片設置為桌面墻紙。
三、AndroidManifest.xml文件
打開AndroidManifest.xml文件,添加:
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
不加的話會背景設置失敗
完整代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hw"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.hw.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
效果如下:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。