温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

OnLongClickListener长按事件设置墙纸

发布时间:2020-07-21 16:27:11 来源:网络 阅读:527 作者:没有水勒鱼 栏目:移动开发

在AndroidApp应用中,OnLongClick事件表示长按2秒以上触发的事件,本章我们通过长按图像设置为墙纸来理解其具体用法。

  知识点:OnLongClickListener
  OnLongClickListener接口与之前介绍的OnClickListener接口原理基本相同,只是该接口为View长按事件的捕捉接口,即当长时间按下某个View时触发的事件,该接口对应的回调方法签名如下。
  public boolean onLongClick(View v) 
  参数v:参数v为事件源控件,当长时间按下此控件时才会触发该方法。
  返回值:该方法的返回值为一个boolean类型的变量,当返回true时,表示已经完整地处理了这个事件,并不希望其他的回调方法再次进行处理;当返回false时,表示并没有完全处理完该事件,更希望其他方法继续对其进行处理。

OnLongClickListener长按事件设置墙纸


一、设计界面

  1、首先把a.jpg、b.jpg、c.jpg、d.jpg、e.jpg、prov.png、next.png图片复制到res/drawable-hdpi文件夹内。

OnLongClickListener长按事件设置墙纸



2、打开“res/layout/activity_main.xml”文件,生成ImageButton按钮。

  (1)从工具栏向activity拖出1个图像ImageView、2个图像按钮ImageButton。该控件来自Image&Media。

OnLongClickListener长按事件设置墙纸



3、打开activity_main.xml文件。

  我们把自动生成的代码修改成如下代码,具体为:

  (1)ImageView的id修改为picture;

  (2)“上一幅”按钮ImageButton的id修改为prov;

  (3)设置android:padding="0dp",按钮灰色边框去掉。

  (4)“下一幅”按钮ImageButton的id修改为next;

  (5)设置android:padding="0dp",按钮灰色边框去掉。

OnLongClickListener长按事件设置墙纸



代码如下:

<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>


效果如下:

OnLongClickListener长按事件设置墙纸




向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI