Timepicker,timepickerdialog and Custom Timepicker (i)

Source: Internet
Author: User
Tags drawtext gety

Scene: Click on activity, Pop up a dialog, then click on the dialog button on the popup time. and Custom Dialog

Lazy, to use a choice of time requirements, only required hours and minutes, pop-up, the first time to think of Timepicker and Numberpicker,

First with Timepicker, found in 4.0 black ugly dead, 4.4 normal want and 5.0 is the pendulum, with

Timepickerdialog than timepicker many mornings and afternoons, forget it or customize it:

Very late, directly post code about the layout, etc. will be studied carefully after studying:

Main activity:

Package Com.example.onebelowzero;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;

public class Mainactivity extends Activity implements onclicklistener{

Private Button mBtn;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Init ();
}

Private Mycustomdialog Dialog;

private void init () {
MBtn = (Button) Findviewbyid (R.ID.BTN);
Mbtn.setonclicklistener (this);
}
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Switch (V.getid ()) {
Case R.ID.BTN:
DoIt ();

Break

Default
Break
}
}

private void DoIt () {
/**
* This style is ugly, we can change the theme in the list file is not necessarily in the appli. Change it and change it on the activity.
* android:theme= "@android: Style/theme.devicedefault.light.noactionbar.fullscreen"
*/
dialog = new Mycustomdialog (this, r.style.customdialog);
Dialog.setcontentview (R.layout.dialog_mycustom);
Dialog.show ();
}

}

=====================

Mycustomdialog:

Package Com.example.onebelowzero;

Import java.util.ArrayList;
Import java.util.List;

Import Android.app.AlertDialog;
Import Android.app.Dialog;
Import Android.app.TimePickerDialog;
Import Android.app.TimePickerDialog.OnTimeSetListener;
Import Android.content.Context;
Import Android.content.DialogInterface;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.TimePicker;
Import Android.widget.Toast;

Import Com.example.onebelowzero.MyPickerView.onSelectListener;

/**
Custom Dialog
*/
public class Mycustomdialog extends Dialog implements android.view.view.onclicklistener{

Private context context;
Private Leavemydialoglistener listener;
Public interface Leavemydialoglistener {
public void OnClick (view view);

public void Refreshui (String timer);
}

Public Mycustomdialog (Context context) {
Super (context);
TODO auto-generated Constructor stub
This.context = context;
}

Public Mycustomdialog (context context, int theme) {
Super (context, theme);
TODO auto-generated Constructor stub
This.context = context;
}

Public Mycustomdialog (context context, int theme, Leavemydialoglistener listener) {
Super (context, theme);
TODO auto-generated Constructor stub
This.context = context;
This.listener = listener;
}


@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);

Initview ();
}

private void Initview () {
TODO auto-generated Method Stub
Button Showpicker = (button) Findviewbyid (r.id.dialog_activity);
Showpicker.setonclicklistener (this);
}

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Datedialog ();
DATEDIALOG2 ();
DateDialog3 ();

}
/**
* Pop-up 4.0,4.4,5.0timepicker styles are different
*/
private void Datedialog () {
Timepickerdialog time = new Timepickerdialog (context,
New Ontimesetlistener () {

@Override
public void Ontimeset (timepicker view, int hourofday,
int minute) {
TODO auto-generated Method Stub
Toast.maketext (Context,
Hourofday + "Hour" + Minute + "minute",
Toast.length_short). Show ();
}
}, (+), true);

Time.show ();
}

/**
* Way two and way one almost
*/
private void DateDialog2 () {
Final Timepicker mdatepicker = new Timepicker (context);

Alertdialog.builder Timedialog = new Alertdialog.builder (context);
Timedialog.settitle ("A Fortress 2012");
Timedialog.seticon (NULL);
Timedialog.setcancelable (FALSE);
Timedialog.setview (Mdatepicker);
Timedialog.setpositivebutton ("OK",
New Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Just take out the Timepicker set time.
Mdatepicker.getcurrenthour ();
Mdatepicker.getcurrentminute ();
}
}). Create ();
Timedialog.show ();
}

/**
* Method Three take a custom view to set the pop-up style to what we want
*/
Mypickerview hour;
Mypickerview minute;
private void DateDialog3 () {
View view = This.getlayoutinflater (). Inflate (R.layout.mypicker_dialog,
NULL);
hour = (Mypickerview) View.findviewbyid (R.ID.MINUTE_PV);
minute = (Mypickerview) View.findviewbyid (R.ID.SECOND_PV);
Create two sets to load the length of our mypickerview inherit view no void ontimechanged (timepicker view, int hourofday, int minute);

list<string> data = new arraylist<string> ();
list<string> seconds = new arraylist<string> ();
for (int i = 0; i < i++) {
Data.add (i<10? ") 0 "+ I:" "+i);//Add 0
}
for (int i = 0; i <; i++) {
Seconds.add (I < 10?) "0" + I: "" + i);
}

Hour.setdata (data);
Hour.setonselectlistener (New Onselectlistener () {

@Override
public void OnSelect (String text) {
TODO auto-generated Method Stub
Get minutes
}
});
Minute.setdata (seconds);
Minute.setonselectlistener (New Onselectlistener () {

@Override
public void OnSelect (String text) {
Toast.maketext (Context, "selected" + text + "points",
Toast.length_short). Show ();

}
});

Alertdialog.builder Timedialog = new Alertdialog.builder (context);
Timedialog.settitle ("A Fortress 2012");
Timedialog.seticon (NULL);
Timedialog.setcancelable (FALSE);
/**
* Add view
*/
Timedialog.setview (view);
Timedialog.setpositivebutton ("OK",
New Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Just take out the Mytimepicker set time.

}
}). Create ();
Timedialog.show ();
}

========================


}

=======================

Mypickerview:::

Package Com.example.onebelowzero;

Import java.util.ArrayList;
Import java.util.List;
Import Java.util.Timer;
Import Java.util.TimerTask;

Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Paint;
Import android.graphics.Paint.Align;
Import android.graphics.Paint.FontMetricsInt;
Import Android.graphics.Paint.Style;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;
Import Android.view.View;

public class Mypickerview extends View {
public static final String TAG = "Pickerview";
/**
* The ratio between text spacing and mintextsize
*/
public static final Float margin_alpha = 2.8f;
/**
* Automatically roll back to the middle of the speed
*/
public static final float speed = 2;

Private list<string> mdatalist;
/**
* Selected position, this position is the center position of the mdatalist, has been unchanged
*/
private int mcurrentselected;
Private Paint Mpaint;

private float mmaxtextsize = 80;
Private float mmintextsize = 40;

private float Mmaxtextalpha = 255;
Private float Mmintextalpha = 120;

private int mcolortext = 0x333333;//text color, casually set

private int mviewheight;
private int mviewwidth;

private float Mlastdowny;
/**
* Sliding distance
*/
private float Mmovelen = 0;
Private Boolean isinit = false;
Private Onselectlistener Mselectlistener;
private timer timer;
Private Mytimertask Mtask;

Handler Updatehandler = new Handler () {

@Override
public void Handlemessage (Message msg) {
if (Math.Abs (Mmovelen) < speed) {
Mmovelen = 0;
if (mtask! = null) {
Mtask.cancel ();
Mtask = null;
Performselect ();
}
} else
Here Mmovelen/math.abs (Mmovelen) is to retain the sign of the Mmovelen to achieve roll up or roll down
Mmovelen = Mmovelen-mmovelen/math.abs (mmovelen) * speed;
Invalidate ();
}

};

Public Mypickerview (Context context) {
Super (context);
Init ();
}

Public Mypickerview (context context, AttributeSet Attrs) {
Super (context, attrs);
Init ();

}

public void Setonselectlistener (Onselectlistener listener) {
Mselectlistener = listener;
}

private void Performselect () {
if (Mselectlistener! = null)
Mselectlistener.onselect (Mdatalist.get (mcurrentselected));
}

public void SetData (list<string> datas) {
Mdatalist = datas;
mcurrentselected = Datas.size ()/2;
Invalidate ();
}

public void setselected (int selected) {
mcurrentselected = selected;
int distance = Mdatalist.size ()/2-mcurrentselected;
if (Distance < 0)
for (int i = 0; i <-distance; i++)
{
Moveheadtotail ();
mcurrentselected--;
}
else if (Distance > 0)
for (int i = 0; i < distance; i++)
{
Movetailtohead ();
mcurrentselected++;
}
Invalidate ();
}

private void Moveheadtotail () {
String head = mdatalist.get (0);
Mdatalist.remove (0);
Mdatalist.add (head);
}

private void Movetailtohead () {
String tail = mdatalist.get (Mdatalist.size ()-1);
Mdatalist.remove (Mdatalist.size ()-1);
Mdatalist.add (0, tail);
}

@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
Super.onmeasure (Widthmeasurespec, Heightmeasurespec);
Mviewheight = Getmeasuredheight ();
Mviewwidth = Getmeasuredwidth ();
Calculates the font size according to the height of the view
Mmaxtextsize = mviewheight/4.0f;
Mmintextsize = mmaxtextsize/2f;
Isinit = true;
Invalidate ();
}

private void init () {
Timer = new timer ();
Mdatalist = new arraylist<string> ();
Mpaint = new Paint (Paint.anti_alias_flag);
Mpaint.setstyle (Style.fill);
Mpaint.settextalign (Align.center);
Mpaint.setcolor (Mcolortext);
}

@Override
protected void OnDraw (canvas canvas) {
Super.ondraw (canvas);
Draw view based on index
if (isinit)
DrawData (canvas);
}

private void DrawData (canvas canvas) {
Draw the selected text first and then draw the rest of the text down
Float scale = parabola (mviewheight/4.0f, Mmovelen);
Float size = (mmaxtextsize-mmintextsize) * scale + mmintextsize;
Mpaint.settextsize (size);
Mpaint.setalpha ((int) ((mmaxtextalpha-mmintextalpha) * scale + mmintextalpha));
Text is centered, note that the baseline is centered and the Y value is the text center coordinate
float x = (float) (mviewwidth/2.0);
Float y = (float) (mviewheight/2.0 + Mmovelen);
Fontmetricsint FMI = Mpaint.getfontmetricsint ();
Float baseline = (float) (Y-(fmi.bottom/2.0 + fmi.top/2.0));

Canvas.drawtext (Mdatalist.get (mcurrentselected), X, Baseline, mpaint);
Draw above data
for (int i = 1; (mcurrentselected-i) >= 0; i++) {
Drawothertext (Canvas, I,-1);
}
Plot the data below
for (int i = 1; (mcurrentselected + i) < mdatalist.size (); i++) {
Drawothertext (Canvas, I, 1);
}
}

/**
* @param canvas
* @param position
* Distance to mcurrentselected difference
* @param type
* 1 means draw down,-1 means draw up
*/
private void Drawothertext (canvas canvas, int position, int type) {
Float d = (float) (Margin_alpha * mmintextsize * position + type
* Mmovelen);
Float scale = parabola (mviewheight/4.0f, D);
Float size = (mmaxtextsize-mmintextsize) * scale + mmintextsize;
Mpaint.settextsize (size);
Mpaint.setalpha ((int) ((mmaxtextalpha-mmintextalpha) * scale + mmintextalpha));
Float y = (float) (mviewheight/2.0 + type * d);
Fontmetricsint FMI = Mpaint.getfontmetricsint ();
Float baseline = (float) (Y-(fmi.bottom/2.0 + fmi.top/2.0));
Canvas.drawtext (Mdatalist.get (mcurrentselected + type * position),
(float) (mviewwidth/2.0), Baseline, mpaint);
}
/**
* Parabolic
*
* @param Zero
* 0 Point coordinates
* @param x
* Offset Amount
* @return Scale
*/
Private float parabola (float zero, float x) {
float f = (float) (1-math.pow (X/zero, 2));
Return F < 0? 0:f;
}
@Override
public boolean ontouchevent (Motionevent event) {
Switch (event.getactionmasked ()) {
Case Motionevent.action_down:
Dodown (event);
Break
Case Motionevent.action_move:
Domove (event);
Break
Case MOTIONEVENT.ACTION_UP:
Doup (event);
Break
}
return true;
}

private void Dodown (Motionevent event) {
if (mtask! = null) {
Mtask.cancel ();
Mtask = null;
}
Mlastdowny = Event.gety ();
}

private void Domove (Motionevent event) {

Mmovelen + = (event.gety ()-mlastdowny);

if (Mmovelen > Margin_alpha * mmintextsize/2) {
To fall more than to leave the distance
Movetailtohead ();
Mmovelen = Mmovelen-margin_alpha * mmintextsize;
} else if (Mmovelen <-margin_alpha * MMINTEXTSIZE/2) {
To slide upwards beyond the distance of departure
Moveheadtotail ();
Mmovelen = Mmovelen + Margin_alpha * mmintextsize;
}

Mlastdowny = Event.gety ();
Invalidate ();
}

private void Doup (Motionevent event) {
The position of the mcurrentselected after lifting the hand from the current position move to the middle selected position
if (Math.Abs (Mmovelen) < 0.0001) {
Mmovelen = 0;
Return
}
if (mtask! = null) {
Mtask.cancel ();
Mtask = null;
}
Mtask = new Mytimertask (Updatehandler);
Timer.schedule (mtask, 0, 10);
}

Class Mytimertask extends TimerTask {
Handler Handler;

Public Mytimertask (Handler Handler) {
This.handler = handler;
}

@Override
public void Run () {
Handler.sendmessage (Handler.obtainmessage ());
}
}

/**
* Define the interface to select
* @author kcw001
*/
Public interface Onselectlistener {
void OnSelect (String text);
}
}

Layout files and so on, see the full code:

http://download.csdn.net/detail/onebelowzero2012/9374740

Timepicker,timepickerdialog and Custom Timepicker (i)

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.