Some small features of Android

Source: Internet
Author: User


1. Image compression

To load a picture from a directory:
Bitmap bm=bitmapfactory.decodefile ("/mnt/sdcard/xxx.bmp"); Load----Original Size
Iv.setimagebitmap (BM); Slow----speed
Compression loading:
When----------compression, you often calculate the compression factor
To get the screen width High:
WindowManager Widow=getwindowmanager ();
int Screenwidth=widow.getdefultdisplay (). GetWidth ();
int Screenheight=widow.getdefultdisplay.getheight ();
Get Picture Width Height:
Options ops=new options ();
Ops.injustdecodebounds=true; Loads only the properties of the picture.
Bitmap bm=bitmapfactory.decodefile ("/mnt/sdcard/xxx.bmp", OPS);
int Bmpwidth=ops.outwidth ();
int Bmpheight=ops.outheight ();
Calculate the compression scale:
int Times=1;
int wtims=bmpwidth/screenwidth;
int htimes=bmpheight/screenheight;
if (wtims>htimes) {times=wtims;}
Else{times=htimes;}

Ops.insamplesize=times; --------Compression ratio
Ops.injustdecodebounds=flase;
Bitmap bm=bitmapfactory.decodefile ("/mnt/sdcard/xxx.bmp", OPS);
Iv.setimagebitmap (BM);

2. Picture copy

public void Copyimage (view view) {
Bitmap BM;
1. Create a blank image
Bitmap Copybm=bitmap.createbitmap (Bm.getwidth (), Bm.getheight,bm.getconfig ());
2. Draw the contents of the old picture bm into the COPYBM
----Call the drawing () class
Canvas canvas=new canvas (COPYBM);
-----Matrix Rectangle Object
Matrix matrix=new Matrix ();
-----Paint Brush
Paint paint=new paint ();
Canvas.drawbitmap (Bm,matrix,paint); -----Matrix,paint

Iv.setimagebitmap (COPYBM);
}

' 3, tear the clothes

Set Transparency:
Bm.setpixel (x,y,color.transparent);

Set two ImageView, to upper Layer IV:
Iv.setontouchlistener (New View.ontouchlistener () {--Listen for click events
public boolean OnTouch (View v,motionevent event) {
int action=event.getaction ();
Switch (action) {
Case MOTIONEVENT.ACTION_UP:
Break
Case Motionevent.action_down:
Break
Case Motionevent.move:
int x= (int) event.getx ();
int y= (int) event.gety ();
for (int xi=x-2;xi<x+2;xi++) {
for (int yi=y-2;yi<y+2;yi++) {
Copyimage,setpixel (xi,yi,color.transparent);
}
}
Break
}
return (ture);
}
});


4. Play Music

MediaPlayer player=new MediaPlayer ();
1. Play
Player.setdatasource ("/.../. /xxx.mp3 ");

Player.setaudiostreamtype (Audiomanager.stream_music); --Set type
Player.prepare (); -------Player Ready
Player.start (); -------Start

The above way, loading from the network may be very slow. So use one-step loading:
Player.prepareasync ();
Player.setonpreparedlistener (New Onpreparedlistener () {
pulic void onprepared (MediaPlayer MP) {
Player.start (); -------set listener, play after loading is complete
}
});

Playerbutton.setenabled (flase); -----Disable the Play button
--Set the listener, when the song is finished, the button is enabled
Player.setoncompletionlistener (New Oncompletionlistener () {
public void Oncompletion (MediaPlayer MP) {
Playerbutton.setenabled (TRUE);
}
});
2. Pause
Player.pause ();
3. Replay
Player.seekto (0); -----jumps to 0 milliseconds. Unit milliseconds.
4. Stop
Player.stop ();
After---Stop, release, set empty
Player.release ();
Player=null;

5. Play Video

Method:
MediaPlayer object, with control <surfaceview/>-----can be used for video, game interface, etc.

public class Mainactivity extends Activity {
Private MediaPlayer player;
Private Surfaceview surface;
Private Surfaceholder sh;
Private Button pausebt;
private int Jindu;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Surface= (Surfaceview) This.findviewbyid (r.id.surface);
pausebt= (Button) This.findviewbyid (r.id.pause);
}

public void player (view view) {
Player=new MediaPlayer ();
try {
Player.setdatasource ("Http://192.168.0.129:8080/fuwuqi/123.mp4");
} catch (Exception e)
{E.printstacktrace ();}
Sh=surface.getholder ();
Sh.addcallback (New Callback () {
@Override
public void surfacechanged (surfaceholder holder, int format,
int width, int height) {
TODO auto-generated Method Stub
SYSTEM.OUT.PRINTLN ("BM");
}
@Override
public void surfacecreated (Surfaceholder holder) {
Sh=surface.getholder ();
Player.setdisplay (SH);
Player.prepareasync ();
Player.setonpreparedlistener (New Onpreparedlistener () {
@Override
public void onprepared (MediaPlayer MP) {
TODO auto-generated Method Stub
Player.seekto (Jindu);
Player.start ();
}
});
}

@Override
public void surfacedestroyed (Surfaceholder holder) {
TODO auto-generated Method Stub
Jindu=player.getcurrentposition ();
Player.stop ();
}

});
Player.setdisplay (SH);
Player.prepareasync ();
Player.setonpreparedlistener (New Onpreparedlistener () {
@Override
public void onprepared (MediaPlayer MP) {
TODO auto-generated Method Stub
Player.start ();

}
});
}
public void Pause (view view) {
if (Pausebt.gettext (). toString (). Equals ("pause")) {
if (player!=null&&player.isplaying ()) {
Player.pause ();
Pausebt.settext ("Continue");}
}else{
Player.start ();
Pausebt.settext ("pause");
}
}
public void Stop (view view) {
if (player!=null)
{player.stop ();
Player.release ();}
}
}

Some small features of Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.