Android image Drawing (iv) Customizing a Surfaceview control _android

Source: Internet
Author: User
Tags null null
Customize a Surfaceview for use with custom controls, such as buttons.
such as a piece of dynamic diagram (custom appropriate), or similar to the UC browser toolbar below.
The following diagram example

Custom Class Code
Copy Code code as follows:

public class Imagesurfaceview extends Surfaceview implements callback{
Used to control Surfaceview
Private Surfaceholder SFH;
Private Handler Handler = new Handler ();
Private imagerunnable imagerunnable = new imagerunnable ();
Private Paint Paint;
Private Canvas Canvas;
Private matrix matrix;

/** Picture Coordinates * *
Private float ImageX, imagey;
/** Get the picture * *
Private Bitmap bmp;
/** picture Wide/high *
Private float BMPW, bmph;
/** Screen Size * *
private int screenw, screenh;

/**
* Surfaceview initialization function
*/
Public Imagesurfaceview (context context, AttributeSet Attrs) {
Super (context, attrs);
SFH = This.getholder ();
Sfh.addcallback (this);
Paint = new paint ();
Paint.setcolor (Color.White);
Paint.setantialias (TRUE);
Setfocusable (TRUE);
}
/**
* Surfaceview View creation, responding to this function
*/
@Override
public void surfacecreated (Surfaceholder holder) {
System.out.println ("Imagesurfaceview is surfacecreated");
Screenh = This.getheight ();
Screenw = This.getwidth ();
Handler.post (imagerunnable);
}
/**
* Game Drawing
*/
public void Draw () {
try {
Canvas = Sfh.lockcanvas ();
Canvas.drawrgb (0, 0, 0);
Canvas.save ();
Draw
Canvas.drawbitmap (BMP, Matrix, paint);
System.out.println ("Draw the image?") ");
Canvas.restore ();
catch (Exception e) {
E.printstacktrace ();
finally {
if (canvas!= null)
Sfh.unlockcanvasandpost (canvas);
}
}
/**
* Touch Screen Event Monitor
*/
@Override
public boolean ontouchevent (Motionevent event) {
return true;
}

/**
* Picture of the thread running
*/
Class Imagerunnable implements runnable{
@Override
public void Run () {
Long start = System.currenttimemillis ();
Draw ();
Long end = System.currenttimemillis ();
if (End-start < 500) {
Handler.postdelayed (This, C-(End-start));
}else{
Handler.post (this);
}
}
}

/**
* Surfaceview view state changed to respond to this function
*/
@Override
public void surfacechanged (surfaceholder holder, int format, int width, int height) {
System.out.println ("Imagesurfaceview is surfacechanged");
}
/**
* Response to this function when the Surfaceview view dies
*/
@Override
public void surfacedestroyed (Surfaceholder holder) {
System.out.println ("Imagesurfaceview is surfacedestroyed");
}
}

The XML code for the layout is as follows (using the method, the full address of the class as the control name):
Copy Code code as follows:

<framelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
>

<akai.test.getimage.imagesurfaceview android:id= "@+id/myimageview"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
/>

<linearlayout android:id= "@+id/buttons"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:orientation= "Horizontal"
Android:background= "@android: Color/white"
>
<button android:id= "@+id/getimage"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Select Picture"
/>
<button android:id= "@+id/getimage_ok"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:text= "OK"
/>
<button android:id= "@+id/getimage_cancle"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:text= "Cancel"
/>
</LinearLayout>

</FrameLayout>

The above code for example, only for reference!
Note the following questions
1, the initialization function of this class need to add parameters, for: Public Imagesurfaceview (context, AttributeSet attrs);
2. Do not get screen width and height when initializing, and do not perform surfacecreated when initializing, so get width and height after surfacecreated or after, and before surfacedestroyed;
3, in the display of this control, will perform surfacecreated and surfacechanged, when jumping to other interfaces when the implementation of the surfacedestroyed (whether or not the current activity has been destroyed), So if you execute Sfh.lockcanvas () immediately when you jump back to the secondary control, you will get null null value.
Related Article

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.