Android for TV: Customize the Sun of the view

Source: Internet
Author: User

Look first.



Package Com.hhzt.iptv.lvb_w8.view;

Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.PixelFormat;
Import Android.graphics.PorterDuff;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.util.AttributeSet;
Import Android.view.SurfaceHolder;
Import Android.view.SurfaceView;
Import Android.view.View;

/**
* Custom Light Select View
*
* @author Chuwe1
*/
public class Lightview extends Surfaceview implements Surfaceholder.callback, Runnable {

Default radius
private static final int default_radius = 170;

Private Surfaceholder Mholder;
Private Canvas Mcanvas;
Private Boolean flag;
/**
* Current Progress
*/
private int mcurrentcount = 0;

Circle and Scale brushes
Private Paint Mpaint;
Pointer brush
Private Paint Mpointerpaint;

The width and height of the canvas
private int mcanvaswidth, mcanvasheight;
Clock radius
private int mradius = Default_radius;


Public Lightview (Context context) {
This (context, NULL);
}

Public Lightview (context context, AttributeSet Attrs) {
This (context, attrs, 0);
}

Public Lightview (context context, AttributeSet attrs, int defstyleattr) {
Super (context, attrs, defstyleattr);

Mholder = Getholder ();
Mholder.addcallback (this);

Mpaint = new Paint ();
Mpointerpaint = new Paint ();

Mpaint.setcolor (Color.White);
Mpaint.setantialias (TRUE); Anti-aliasing
Mpaint.setstrokewidth (10); Set the width of the ring
Mpaint.setstrokecap (Paint.Cap.ROUND); Defines the segment power cut shape as a round head
Mpaint.setantialias (TRUE); Anti-aliasing



Mpointerpaint.setcolor (Color.Black);
Mpointerpaint.setantialias (TRUE);
Mpointerpaint.setstyle (Paint.Style.FILL_AND_STROKE);
Mpointerpaint.settextsize (22);
Mpointerpaint.settextalign (Paint.Align.CENTER);

Setzorderontop (TRUE);
Getholder (). SetFormat (pixelformat.translucent);

Setfocusable (TRUE);
Setfocusableintouchmode (TRUE);
}

@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
Super.onmeasure (Widthmeasurespec, Heightmeasurespec);

int widthsize = measurespec.getsize (Widthmeasurespec);
int widthmode = Measurespec.getmode (Widthmeasurespec);
int heightsize = measurespec.getsize (Heightmeasurespec);
int heightmode = Measurespec.getmode (Heightmeasurespec);

int Desiredwidth, desiredheight;
if (Widthmode = = measurespec.exactly) {
Desiredwidth = widthsize;
} else {
Desiredwidth = Mradius * 2 + getpaddingleft () + getpaddingright ();
if (Widthmode = = Measurespec.at_most) {
Desiredwidth = Math.min (widthsize, desiredwidth);
}
}

if (Heightmode = = measurespec.exactly) {
Desiredheight = heightsize;
} else {
Desiredheight = Mradius * 2 + getpaddingtop () + Getpaddingbottom ();
if (Heightmode = = Measurespec.at_most) {
Desiredheight = Math.min (heightsize, desiredheight);
}
}

+4 is to set the default 2px padding, because the brush setting of the circle that draws the clock is set to a width of 2px
Setmeasureddimension (mcanvaswidth = Desiredwidth +, Mcanvasheight = desiredheight + 20);

Mradius = (int) (Math.min (Desiredwidth-getpaddingleft ()-Getpaddingright (),
Desiredheight-getpaddingtop ()-Getpaddingbottom ()) * 1.0F/2);
}

@Override
public void surfacecreated (Surfaceholder holder) {
Flag = true;
New Thread (This). Start ();
}

@Override
public void surfacechanged (surfaceholder holder, int format, int width, int height) {
}

@Override
public void surfacedestroyed (Surfaceholder holder) {
Flag = false;
}

@Override
public void Run () {
Draw ();
}

Private Handler Handler = new Handler (new Handler.callback () {
@Override
public boolean handlemessage (Message msg) {
return false;
}
});


/**
* Draw
*/
private void Draw () {
try {
Mcanvas = Mholder.lockcanvas ();
if (Mcanvas! = null) {
Remove background color to make it transparent
Mcanvas.drawcolor (Color.transparent, PorterDuff.Mode.CLEAR);

Mpaint.setcolor (Color.White);
Move the coordinate system origin to the center of the canvas after the inner margin is removed
Mcanvas.translate (Mcanvaswidth * 1.0F/2 + getpaddingleft ()-Getpaddingright (),
Mcanvasheight * 1.0F/2 + getpaddingtop ()-getpaddingbottom ());
Draw a disc
Mcanvas.drawcircle (0, 0, mRadius-30, mpaint);
Mpaint.setcolor (Color.White);
Draw a Time degree
for (int i = 0; i < i++) {
Mcanvas.drawline (0, Mradius, 0, mRadius-10, mpaint);
Mcanvas.rotate (36);
}
Mpaint.setcolor (Color.parsecolor ("#FEF37A"));
Draw a Time degree
for (int i = 0; i < Mcurrentcount; i++) {
Mcanvas.drawline (0,-mradius, 0,-(mRadius-10), mpaint);
Mcanvas.rotate (36);
}
}
} catch (Exception e) {
E.printstacktrace ();
} finally {
if (Mcanvas! = null) {
Mholder.unlockcanvasandpost (Mcanvas);
}
}
}

public void Setcurrentcount (int count) {
This.mcurrentcount = count;
Invalidate ();
}

@Override
protected void OnLayout (Boolean changed, int left, int top, int. right, int bottom) {
Super.onlayout (changed, left, top, right, bottom);
This.setbackgroundcolor (color.transparent);
}


}

Android for TV: Customize the Sun of the view

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.