In the androidapp, The onlongclick event indicates an event triggered by a long press for more than 2 seconds. In this chapter, we use the image as a wallpaper to understand its usage.
Knowledge Point: onlongclicklistener
The onlongclicklistener interface is basically the same as the onclicklistener interface previously introduced. It is only the capture interface of the view long-pressed event, that is, the event triggered when a view is pressed for a long time, the callback method signature corresponding to this interface is as follows.
Public Boolean onlongclick (view V)
Parameter V: parameter V is the event source control. This method is triggered only when the control is pressed for a long time.
Return Value: the return value of this method is a Boolean variable. If true is returned, it indicates that the event has been fully processed and other callback methods are not expected to be processed again; if false is returned, it indicates that the event is not completely processed, and other methods are expected to continue processing the event.
650) This. width = 650; "src =" http://img.blog.csdn.net/20151028150325218 "alt =" 20151028150325218 "/>
I. Design Interface
1. Copy the.jpg‑b.jpg‑c.jpg‑d.jpg‑e.jpg‑prov.png‑next.png image to the res/drawable-hdpi folder.
650) This. width = 650; "src =" http://img.blog.csdn.net/20151028150356287 "alt =" 20151028150356287 "/>
2. Open the "Res/layout/activity_main.xml" file and generate the imagebutton button.
(1) drag an image imageview and two image buttons imagebutton from the toolbar to the activity. This control is from Image & Media.
650) This. width = 650; "src =" http://img.blog.csdn.net/20151028150447860 "alt =" 20151028150447860 "/>
3. Open the activity_main.xml file.
Modify the automatically generated code to the following code:
(1) Change the imageview ID to picture;
(2) Change the ID of imagebutton to prov;
(3) set Android: padding = "0dp" and remove the gray border of the button.
(4) change the ID of imagebutton to next;
(5) set Android: padding = "0dp" and remove the gray border of the button.
650) This. width = 650; "src =" http://img.blog.csdn.net/20151028150659647 "alt =" 20151028150659647 "/>
The Code is as follows:
<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>
Ii. Long press event
Open the src/COM. genwoxue. onlongclick/mainactivity. Java file.
Enter the following code:
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;
// Declare 5 images
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 );
// Register The onclick listener
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]);
}
}
});
// Register The onclick listener
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]);
}
}
});
// Register the onlongclick listener
Ivwpicture. setonlongclicklistener (New onlongclicklistener (){
@ Override
Public Boolean onlongclick (view ){
Try {
// Clear the current Wallpaper
Mainactivity. This. clearwallpaper ();
// Convert the current view to an imageview object
Imageview IV = (imageview) view;
// Enable image Buffering
Iv. setdrawingcacheenabled (true );
// Use the current buffer image to create a bitmap
Bitmap BMP = bitmap. createbitmap (IV. getdrawingcache ());
// Set the current image as a wallpaper
Mainactivity. This. setwallpaper (BMP );
// Clear the Image Buffer
Iv. setdrawingcacheenabled (false );
Toast. maketext (getapplicationcontext (), "", Toast. length_long). Show ();
} Catch (exception e ){
Toast. maketext (getapplicationcontext (), "failed to set the background", Toast. length_long). Show ();
}
Return true;
}
});
}
@ Override
Public Boolean oncreateoptionsmenu (menu ){
// Inflate the menu; this adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R. Menu. Main, menu );
Return true;
}
}
You can use the "previous" and "Next" buttons to browse images. You can set the current image as a desktop Wallpaper by pressing the image.
3. androidmanifest. xml file
Open the androidmanifest. xml file and add:
<Uses-Permission Android: Name = "android. Permission. set_wallpaper"/>
If this parameter is not added, the background setting will fail.
The complete code is as follows:
<? 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" type = "codeph" text = "/codeph">
<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>
The effect is as follows:
650) This. width = 650; "src =" http://img.blog.csdn.net/20151028151355226 "alt =" 20151028151355226 "/>
This article is from the "no fish" blog, please be sure to keep this source http://javaqun.blog.51cto.com/10687700/1707274
Onlongclicklistener long-Press the event to set the wallpaper