在Android App應用中,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按鈕。
(1)從工具欄向activity拖出1個圖像ImageView、2個圖像按鈕ImageButton。該控件來自Image&Media。
3、打開activity_main.xml文件。
我們把自動生成的代碼修改成如下代碼,具體爲:
(1)ImageView的id修改爲picture;
(2)“上一幅”按鈕ImageButton的id修改爲prov;
(3)設置android:padding="0dp",按鈕灰色邊框去掉。
(4)“下一幅”按鈕ImageButton的id修改爲next;
(5)設置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"
- tools:context=".MainActivity" >
- <ImageView
- android:id="@+id/picture"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="18dp"
- android:src="@drawable/a"
- tools:ignore="ContentDescription" />
- <ImageButton
- android:id="@+id/prov"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_below="@+id/picture"
- android:layout_marginLeft="38dp"
- android:layout_marginTop="16dp"
- android:padding="0dp"
- android:src="@drawable/prov" />
- <ImageButton
- android:id="@+id/next"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_alignTop="@+id/prov"
- android:layout_marginRight="24dp"
- android:padding="0dp"
- android:src="@drawable/next" />
- </RelativeLayout>
4、界面如下:
二、長按事件
打開“src/com.genwoxue.onlongclick/MainActivity.java”文件。
然後輸入以下代碼:
- package com.example.onlongclick;
- import android.os.Bundle;
- import android.app.Activity;
- import android.widget.ImageButton;
- import android.widget.ImageView;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.View.OnLongClickListener;
- import android.widget.Toast;
- import android.graphics.Bitmap;
- public class MainActivity extends Activity {
- //聲明Image對象與ImageBoutton對象
- 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);
- //獲取ImageView對象ivwPicture
- ivwPicture=(ImageView)super.findViewById(R.id.picture);
- //獲取兩個按鈕對象ImageButton
- ibtnProv=(ImageButton)super.findViewById(R.id.prov);
- ibtnNext=(ImageButton)super.findViewById(R.id.next);
- //註冊OnClick監聽器
- ibtnProv.setOnClickListener(new ProvOnClickListener());
- ibtnNext.setOnClickListener(new NextOnClickListener());
- //註冊OnlongClick監聽器
- ivwPicture.setOnLongClickListener(new PicOnLongClick());
- }
- //單擊“上一幅”按鈕顯示前一張圖片
- private class ProvOnClickListener implements OnClickListener{
- private int i=5;
- public void onClick(View view){
- if(i > 0){
- ivwPicture.setImageResource(iImages[--i]);
- }
- else if(i == 0){
- i =5;
- ivwPicture.setImageResource(iImages[4]);
- }
- }
- }
- //單擊“下一幅”按鈕顯示後一張圖片
- private class NextOnClickListener implements OnClickListener{
- private int i=0;
- public void onClick(View view){
- if(i < 5)
- ivwPicture.setImageResource(iImages[i++]);
- else if(i == 5){
- i = 0;
- ivwPicture.setImageResource(iImages[0]);
- }
- }
- }
- //長按圖片設置爲桌面牆紙
- private class PicOnLongClick implements 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;
- }
- }
- }
通過“上一幅”、“下一幅”按鈕瀏覽圖片,長按圖片可以把當前圖片設置爲桌面牆紙。
三、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.onlongclick"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="15" />
- <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.onlongclick.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>
效果如下: