使用自定义imageview中的图像拖动矩形,而不使用矩阵Android

时间:2013-02-23 09:23:12

标签: android math imageview

我必须做一个项目,我有一个图像(如地板架构),我可以在这个图像上绘制矩形。

我设法绘制图像,缩放和拖动。 我设法在这个图像上绘制矩形并使用相同的图像比例进行缩放。

所以我的主要问题是如何用图像移动那些矩形。不使用矩阵。

我使用图像的中心作为几何地标,它可能是错误的。

感谢您的帮助。

MyAcitvity

public class ZoneActivity extends Activity implements OnItemSelectedListener, OnSeekBarChangeListener {
private TextView tv;
private View cadre;
ArrayList<RiskyZone> listRz;
private FloorImageView floorImage;
private final FloorMapper floormapper;
private Menu menu;
private SubMenu editRiskyZone;
private SubMenu editWard;

private SeekBar zoomBar; 

public ZoneActivity(FloorMapper floormapper){
    this.floormapper=floormapper;
}

public ZoneActivity(){
    this.floormapper=new FloorMapper();
    this.floormapper.addFloor(new Floor("floor0"));
    this.floormapper.addFloor(new Floor("floor1"));
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Spinner s = (Spinner) findViewById(R.id.zone_spinner);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, floormapper.getFloorNames());
    s.setAdapter(adapter);
    s.setOnItemSelectedListener(this);



    this.tv = (TextView) findViewById(R.id.testID);
    this.tv.setText("TEST2");
    this.tv.setTextColor(Color.rgb(200,0,0));

    listRz = new ArrayList<RiskyZone>();
    RiskyZone rz1 = new RiskyZone(Color.RED, 1, "toto");
    //rz1.addRectangle(new ZoneRectangle(new Coordinates(10, 40), new Coordinates(50, 50)));    
    rz1.addRectangle(new ZoneRectangle(new Coordinates(30, 50), new Coordinates(100, 100)));    

    RiskyZone rz2 = new RiskyZone(Color.BLUE, 2, "tata");
    rz2.addRectangle(new ZoneRectangle(new Coordinates(0, 0), new Coordinates(50, 60)));    


    listRz.add(rz1);
    listRz.add(rz2);



    this.floorImage = (FloorImageView) findViewById(R.id.zone_image);
    this.floorImage.setRiskyZones(listRz);

    //savoir la taille de l'image charge
    ViewTreeObserver vto = floorImage.getViewTreeObserver();
    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {
            int finalHeight = floorImage.getMeasuredHeight();
            int finalWidth = floorImage.getMeasuredWidth();
            //tv.setText("Height: " + finalHeight + " Width: " + finalWidth);
            return true;
        }
    });


    this.zoomBar = (SeekBar) findViewById(R.id.zoomBar);
    this.zoomBar.setOnSeekBarChangeListener(this);


}

/**
 * permet de changer de menu lorsque l'on change de floor
 * permet d'afficher que les zone a risque presente sur l'etage en cours de visu
 */
private void onChangeFloorChangeMenu(){
    if(editRiskyZone != null){
        editRiskyZone.clear();
        Floor floor = floormapper.getFloorByName(tv.getText().toString());

        // add menu items to the submenu
        for (RiskyZone rz : listRz) {
            //voir a ajoute une couleur sur chaque ligne
            editRiskyZone.add(R.string.edit_risky_zone, rz.getId(), Menu.NONE, rz.getName());
        }

        editWard.clear();
        //TODO:Ajouter les ward de l'etage dans le menu
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //Creation des menus de base
    editRiskyZone = menu.addSubMenu(R.string.edit_risky_zone);
    editWard = menu.addSubMenu(R.string.edit_ward);
    return true;
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    String floorName = (String)parent.getItemAtPosition(pos);
    this.tv.setText((CharSequence) parent.getItemAtPosition(pos));
    int resId = getResources().getIdentifier(floorName, "drawable", getPackageName());
    this.floorImage.setImageResource(resId);
    onChangeFloorChangeMenu();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    this.floorImage.setZoom(progress);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

}
}

我的自定义ImageView

public class FloorImageView extends ImageView implements OnTouchListener {
private Drawable image;
ImageButton img,img1;
private int zoomControler;
private int oldZoomControler;
private ArrayList<RiskyZone> riskyZones = new ArrayList<RiskyZone>();

PointF last = new PointF();
PointF start = new PointF();

private float translateX;
private float translateY;

private int left;
private int top;
private int right;
private int bottom;


public FloorImageView(Context context, AttributeSet attrset){
    super(context, attrset);
    image = context.getResources().getDrawable(R.drawable.floor0);
    setFocusable(true);
    setOnTouchListener(this);
}

public ArrayList<RiskyZone> getRiskyZones() {
    return riskyZones;
}

public void setRiskyZones(ArrayList<RiskyZone> riskyZones) {
    this.riskyZones = riskyZones;
}

public void drawZoneRectangles(Canvas canvas, ArrayList<Integer> riskyZoneFilters){
    for(RiskyZone r : riskyZones){
        canvas.save();
        ArrayList<ZoneRectangle> rectangles = (ArrayList<ZoneRectangle>) r.getRectangles();
        Paint paint = new Paint();
        paint.setAlpha(80);
        paint.setColor(r.getColor());
        for(ZoneRectangle rect : rectangles){

            float ratioX = (float)((right+oldZoomControler)-(left-oldZoomControler))/(float)((right+(getWidth()/2))-(left-(getWidth()/2)));
            float ratioY = (float)((bottom+oldZoomControler)-(top-oldZoomControler))/(float)((bottom+(getWidth()/2))-(top-(getWidth()/2)));


            float xDistance = (rect.getCooSE().getX()-rect.getCooNW().getX());
            float yDistance = (rect.getCooSE().getY()-rect.getCooNW().getY());
            System.out.println("xDistance = " + xDistance + " :::   yDistance = " +  yDistance);

            float xCenter = (rect.getCooNW().getX() + (xDistance/2)) + this.left - (rect.getCooNW().getX() + this.left);
            float yCenter = (rect.getCooNW().getY() + (yDistance/2)) + this.bottom - (rect.getCooNW().getY() + this.bottom);
            System.out.println("xCenter = " + xCenter + " yCenter = " +  yCenter);

            canvas.drawRect(xCenter-((xDistance/2)*ratioX), yCenter-((yDistance/2)*ratioY), 
                    xCenter+((xDistance/2)*ratioX), yCenter+((yDistance/2)*ratioY) , paint);
        }
        canvas.restore();
    }
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(0xff000000);

    image.setBounds(left-zoomControler, top-zoomControler, right+zoomControler, bottom+zoomControler);
    image.draw(canvas);
    drawZoneRectangles(canvas, new ArrayList<Integer>());

}


public void setZoom(int zoom){
    System.out.println(zoom);
    this.oldZoomControler = this.zoomControler;
    this.zoomControler = (getWidth()/2) + (zoom*10);
    invalidate();
}

@Override
public void setImageResource(int resId) {
    super.setImageResource(resId);
    this.image = getResources().getDrawable(resId);
    this.zoomControler = getWidth()/2;
    this.left = (getWidth()/2);
    this.top = (getHeight()/2);
    this.right = (getWidth()/2);
    this.bottom = (getHeight()/2);
}


@Override
public boolean onTouch(View v, MotionEvent event) {
    PointF curr = new PointF(event.getX(), event.getY());

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        last.set(curr);
        start.set(last);

        break;

    case MotionEvent.ACTION_MOVE:
        float deltaX = curr.x - last.x;
        float deltaY = curr.y - last.y;

        translateX = deltaX;
        translateY = deltaY;

        this.left = this.left + (int) deltaX;
        this.top = this.top + (int) deltaY;
        this.right = this.right + (int) deltaX;
        this.bottom = this.bottom + (int) deltaY;

        last.set(curr.x, curr.y);
        break;
    case MotionEvent.ACTION_UP:
        break;

    case MotionEvent.ACTION_POINTER_UP:
        break;
    }

    invalidate();
    return true;
}
}

XML活动

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
xmlns:app="fr.umlv.assistoid.zoning"
android:layout_height="fill_parent"
android:background="#000000"
android:layout_centerHorizontal="true"
android:orientation="vertical" >

<Spinner
    android:id="@+id/zone_spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:background="#FFFFFF"/>

<TextView
    android:id="@+id/testID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="floor0"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<fr.umlv.assistoid.zoning.FloorImageView
    android:id="@+id/zone_image"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/floor0" 
    android:layout_weight="2">
</fr.umlv.assistoid.zoning.FloorImageView>

<SeekBar
    android:id="@+id/zoomBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>

0 个答案:

没有答案