您好,登錄后才能下訂單哦!
在AndroidApp應(yīng)用中,OnLongClick事件表示長按2秒以上觸發(fā)的事件,本章我們通過長按圖像設(shè)置為墻紙來理解其具體用法。
知識點(diǎn):OnLongClickListener
OnLongClickListener接口與之前介紹的OnClickListener接口原理基本相同,只是該接口為View長按事件的捕捉接口,即當(dāng)長時間按下某個View時觸發(fā)的事件,該接口對應(yīng)的回調(diào)方法簽名如下。
public boolean onLongClick(View v)
參數(shù)v:參數(shù)v為事件源控件,當(dāng)長時間按下此控件時才會觸發(fā)該方法。
返回值:該方法的返回值為一個boolean類型的變量,當(dāng)返回true時,表示已經(jīng)完整地處理了這個事件,并不希望其他的回調(diào)方法再次進(jìn)行處理;當(dāng)返回false時,表示并沒有完全處理完該事件,更希望其他方法繼續(xù)對其進(jìn)行處理。
一、設(shè)計(jì)界面
1、首先把a(bǔ).jpg、b.jpg、c.jpg、d.jpg、e.jpg、prov.png、next.png圖片復(fù)制到res/drawable-hdpi文件夾內(nèi)。
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;
(3)設(shè)置android:padding="0dp",按鈕灰色邊框去掉。
?。?)“下一幅”按鈕ImageButton的id修改為next;
?。?)設(shè)置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);
//注冊O(shè)nClick監(jiān)聽器
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]);
}
}
});
//注冊O(shè)nClick監(jiān)聽器
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]);
}
}
});
//注冊O(shè)nlongClick監(jiān)聽器
ivwPicture.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
try{
//清空當(dāng)前墻紙
MainActivity.this.clearWallpaper();
//當(dāng)前view轉(zhuǎn)換為ImageView對象
ImageView iv = (ImageView) view;
//啟用圖形緩沖
iv.setDrawingCacheEnabled(true);
//使用當(dāng)前緩沖圖形創(chuàng)建Bitmap
Bitmap bmp = Bitmap.createBitmap(iv.getDrawingCache());
//當(dāng)前圖片設(shè)置為墻紙
MainActivity.this.setWallpaper(bmp);
//清理圖形緩沖
iv.setDrawingCacheEnabled(false);
Toast.makeText(getApplicationContext(), "背景設(shè)置陳功", Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(getApplicationContext(), "背景設(shè)置失敗", 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;
}
}
通過“上一幅”、“下一幅”按鈕瀏覽圖片,長按圖片可以把當(dāng)前圖片設(shè)置為桌面墻紙。
三、AndroidManifest.xml文件
打開AndroidManifest.xml文件,添加:
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
不加的話會背景設(shè)置失敗
完整代碼如下:
<?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>
效果如下:
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。