美文网首页
对象容器

对象容器

作者: 一个爱上浪漫的人 | 来源:发表于2017-03-27 10:49 被阅读0次

    import android.view.*;

    import android.graphics.*;

    public class 对象容器 extends 对象{

    private int downx=0,downy=0;

    private int[] dxX,dxY;

    public boolean w=true,h=true;

    public Bitmap 背景=null;

    public int 背景颜色=Color.argb(255,100,100,100);

    public int 对象间距=10;

    public Paint 边框=null;

    public 对象容器(int x,int y,int width,int height){

    super(x,y,width,height);

    类名="对象容器";

    }

    public void 显示(Canvas c){

    if(显示)行为(c);

    };

    public void 事件(MotionEvent e){

    if(事件){

    行为(e);

    运行内部对象(e);

    if(e.getAction()==MotionEvent.ACTION_UP){

    移动=false;

    按下=false;

    }

    }

    }

    public void 行为(MotionEvent e){

    if(e.getAction()==MotionEvent.ACTION_DOWN){

    按下=计算.如果在某个矩形范围(this,e);

    if(按下){

    记录内部对象位置();

    downx=(int)e.getX();

    downy=(int)e.getY();

    }

    }

    if(e.getAction()==MotionEvent.ACTION_MOVE){

    if(按下){

    if(计算.绝对值((int)e.getX(),downx)>10||计算.绝对值((int)e.getY(),downy)>10)

    移动=true;

    if(内部对象!=null){

    int x=(int)e.getX()-downx;

    int y=(int)e.getY()-downy;

    移动内部对象(x,y);

    }

    }

    }

    if(e.getAction()==MotionEvent.ACTION_UP){

    if(按下){

    dxX=null;

    dxY=null;

    }

    }

    }

    public void 行为(Canvas c){

    drawbeijing(c,背景颜色);

    }

    public void drawbeijing(Canvas c,int color){

    Bitmap bj=Bitmap.createBitmap(宽,高,Bitmap.Config.ARGB_8888);

    Canvas cc=new Canvas(bj);

    if(背景!=null)cc.drawBitmap(背景,0,0,null);

    cc.drawColor(color);

    if(内部对象!=null&&内部对象显示)运行内部对象(cc);

    if(边框!=null)图形.画矩形(0,0,宽-1,高-1,cc,边框);

    c.drawBitmap(bj,x,y,null);

    }

    public void 移动内部对象(int x,int y){

    int i=0;

    if(内部对象!=null&&dxX!=null&&dxY!=null)

    while(i<内部对象.length){

    if(w)内部对象[i].x=dxX[i]+x;

    if(h)内部对象[i].y=dxY[i]+y;

    i++;

    }

    }

    public void 记录内部对象位置(){

    int i=0;

    if(内部对象!=null)

    while(i<内部对象.length){

    dxX=合并.合并(dxX,内部对象[i].x);

    dxY=合并.合并(dxY,内部对象[i].y);

    i++;

    }

    }

    public void 运行内部对象(MotionEvent e){

    e.setLocation(e.getX()-x,e.getY()-y);

    if(按下)super.运行内部对象(e);

    e.setLocation(e.getX()+x,e.getY()+y);

    }

    public void X方向装入对象(对象 dx){

    if(内部对象==null){

    dx.x=0;

    装入对象(dx);

    }else{

    dx.x=内部对象[内部对象.length-1].x+内部对象[内部对象.length-1].宽+对象间距;

    装入对象(dx);

    }

    }

    public void Y方向装入对象(对象 dx){

    if(内部对象==null){

    dx.y=0;

    装入对象(dx);

    }else{

    dx.y=内部对象[内部对象.length-1].y+内部对象[内部对象.length-1].高+对象间距;

    装入对象(dx);

    }

    }

    public void 设置边框(int 宽度,int 颜色){

    边框=new Paint();

    边框.setStyle(Paint.Style.STROKE);

    边框.setTextSize(宽度);

    边框.setColor(颜色);

    }

    }

    顾名思义这个类是一个对象的容器,一个对象通过装入对象这个方法装入这个容器,可以通过触控移动容器里的所有对象,容器里的对象坐标保持相对不变。

    相关文章

      网友评论

          本文标题:对象容器

          本文链接:https://www.haomeiwen.com/subject/azolottx.html