Implementation of the Android curriculum

Source: Internet
Author: User

The code snippet basically implements the basic layout that the curriculum will display

Package Com.example.android_draw;

Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Matrix;
Import Android.graphics.Paint;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;
Import Android.view.View;

public class Mydraw extends View {
Private paint paintlinev=new paint ();
Private paint paintlineh=new paint ();
Private paint paintjia=new paint ();
Private paint paintnumber=new paint ();
Private paint painttext=new paint ();
Private paint paintselect=new paint ();
private float Mousex,mousey;
private static Boolean initflag=false;
private static Boolean ontoucheventflag=false;
private static int spacew=0;
private static int spaceh=0;
private static int[] result=new int[2];
Private Bitmap Tianjia;
Private String
Public Mydraw (Context context) {
Super (context);
TODO auto-generated Constructor stub
}
Public Mydraw (context context, AttributeSet Attrs) {
Super (context, attrs);
TODO auto-generated Constructor stub
}

Public Mydraw (context context, AttributeSet attrs, int defstyle) {
Super (context, attrs, Defstyle);
TODO auto-generated Constructor stub
}
@Override
protected void OnDraw (canvas canvas) {
TODO auto-generated Method Stub
Super.ondraw (canvas);
if (!initflag) {
Init ();
}
Drawbackground (canvas);
if (Ontoucheventflag) {
Drawrectangle1 (canvas);
}
}
@Override
public boolean ontouchevent (Motionevent event) {
TODO auto-generated Method Stub
Mousex=event.getx ();
Mousey=event.gety ();
Switch (event.getaction ()) {
Case Motionevent.action_down:
if (MOUSEX>SPACEW) {
result[0]= (int) ((MOUSEX-SPACEW)/(2*SPACEW));
Ontoucheventflag=true;
}
result[1]= (int) (mousey/(SPACEH));

Break
}
Mydraw.this.postInvalidate ();
return true;
}

public void Drawbackground (canvas canvas) {
Canvas.drawline (spacew, 0, Spacew, 12*SPACEH, Paintlinev);
for (int i=1;i<13;i++) {
Canvas.drawtext (i, SPACEW/2, SPACEH*I*3/4, Paintnumber);
}
for (int i=1;i<=12;i+=1) {
Canvas.drawline (0, I*spaceh, Spacew, I*SPACEH, Paintlineh);
}
for (int i=1;i<=12;i+=1) {
for (int j=3;j<15;j+=2) {
Canvas.drawline (J*spacew-2, I*spaceh, J*spacew+3, I*SPACEH, Paintlineh);

}
}
for (int i=1;i<=12;i+=1) {
for (int j=3;j<15;j+=2) {
Canvas.drawline (J*spacew, I*spaceh-2, J*spacew, i*spaceh+3, Paintlinev);
}
}
}

private void Drawrectangle1 (canvas canvas) {
//matrix matrix = new Matrix ();
Matrix.prescale (Space/tianjia.getwidth (), Space/tianjia.getheight (), Result[0]*space, result[1]*space);
//canvas.drawbitmap (Tianjia, Matrix, Paintjia);
Canvas.drawbitmap (Tianjia, Result[0]*2*spacew+spacew, Result[1]*spaceh,paintjia);
}
public void init () {
Paintlineh.setcolor (color.black);
Paintlineh.setstrokewidth (1);
Paintlinev.setcolor (Color.Black);
Paintlinev.setstrokewidth (1);
Paintnumber.setcolor (Color.Black);
Paintnumber.setstrokewidth (1);

Tianjia=bitmapfactory.decoderesource (getresources (), R.drawable.tianjia);
int Width=getwidth ();
int height=getheight ();

Spacew=width/15;

Spaceh=height/12;

Paintnumber.settextsize ((float) (0.75*height));
Paintnumber.settextalign (Paint.Align.CENTER);
Tianjia=bitmapfactory.decoderesource (Getresources (), R.drawable.tianjia);
Tianjia=bitmap.createscaledbitmap (Tianjia, 2*spacew, SPACEH, false);
Initflag=true;
}

}

Other content can be added according to their own interests, the following is I do, for reference only.

Implementation of the Android curriculum

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.