Android - 自定义View中的上下文菜单,带有画布

时间:2014-10-06 16:13:54

标签: android view onclick zoom contextmenu

我正在申请处理家庭自动装置 - 灯光,大门,加热等。我已经制作了自定义视图,它可以显示地板的地图,可以缩放和平移,带有图标点击时改变的灯泡。我尝试制作上下文菜单,这将允许我设置例如室温,但是当我长按视图时 - 没有任何反应。我尝试了很多方法,到处寻找,最后我决定请你们帮忙。

以下是代码:

public class ZoomView extends View {

   private float scaleFactor = 1f;
   private ScaleGestureDetector detector;

   private static int NONE = 0;
   private static int DRAG = 1;
   private static int ZOOM = 2;
   private int mode;
   public boolean dragged = false;

   private float startX = 0f;
   private float startY = 0f;

   private float translateX = 0f;
   private float translateY = 0f;

   private float previousTranslateX = 0f;
   private float previousTranslateY = 0f;

   Resources res = getResources();
   Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.floor);

   DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
   float density = getResources().getDisplayMetrics().density;

   private  float MIN_ZOOM = 0.1f;
   private  float MAX_ZOOM = 5f;


   Bitmap lb1a = BitmapFactory.decodeResource(res, R.drawable.lb1a);
   RectF lb1Rect;
   RectF lb1draw = new RectF((64*density), (116*density), (92*density) , (146*density));
   boolean lb1on = false;

   Bitmap lb1b = BitmapFactory.decodeResource(res, R.drawable.lb1b);



   public ZoomView(Context context, AttributeSet attributeSet) {
       super(context, attributeSet);
       detector = new ScaleGestureDetector(getContext(), new ScaleListener());
       this.setLongClickable(true);
       this.setOnLongClickListener(vLong);


   }



   @Override
   public boolean onTouchEvent(MotionEvent event) {


       final int action = event.getAction();
       switch (action & MotionEvent.ACTION_MASK) {
           case MotionEvent.ACTION_DOWN:
               mode = DRAG;
               startX = event.getX() - previousTranslateX;
               startY = event.getY() - previousTranslateY;
               break;

           case MotionEvent.ACTION_MOVE:
               translateX = event.getX() - startX;
               translateY = event.getY() - startY;

               double distance = Math.sqrt(Math.pow(event.getX() - (startX + previousTranslateX), 2) + Math.pow(event.getY() - (startY + previousTranslateY), 2));
              if (distance > 10) {
                dragged = true;
               }
              break;


           case MotionEvent.ACTION_POINTER_DOWN:
               mode = ZOOM;
               break;
           case MotionEvent.ACTION_UP:
               mode = NONE;
               previousTranslateX = translateX;
               previousTranslateY = translateY;
               if (!dragged) {
                   executeClick(startX, startY);
               }

               dragged = false;
               break;

           case MotionEvent.ACTION_POINTER_UP:
               mode = DRAG;
               previousTranslateX = translateX;
               previousTranslateY = translateY;
               break;

       }
       detector.onTouchEvent(event);

       if ((mode == DRAG && dragged) || mode == ZOOM) {
           invalidate();
       }
       return true;

   }


   Paint paint = new Paint();
   Paint black = new Paint();

   @Override
   public void onDraw(Canvas canvas) {
       super.onDraw(canvas);
       canvas.save();
       canvas.drawPaint(paint);
       black.setColor(Color.BLACK);
       black.setStyle(Paint.Style.FILL);
       paint.setColor(Color.BLACK);
       paint.setTextSize(30);

       canvas.scale(this.scaleFactor, this.scaleFactor);
       canvas.translate(translateX/scaleFactor, translateY/scaleFactor);

       lb1Rect = new RectF((64*density)*scaleFactor, (116*density)*scaleFactor,    (92*density)*scaleFactor , (146*density)*scaleFactor);
       canvas.drawBitmap(bmp, 0, 0, null);


       if (lb1on) {
           canvas.drawBitmap(lb1b, null ,lb1draw , null);
       }
       else {
           canvas.drawBitmap(lb1a, null ,lb1draw , null);
       }


       canvas.drawText("translate X:" + Float.toString(translateX), 0, 40, paint);
       canvas.drawText("translate Y:"+ Float.toString(translateY), 0, 90, paint);
       canvas.drawText("scale factor:"+ Float.toString(scaleFactor), 0, 140, paint);

       canvas.restore();
   }

   private OnLongClickListener vLong = new OnLongClickListener() {
       @Override
       public boolean onLongClick(View view) {
           showContextMenu();
           lb1on=!lb1on;
           return true;
       }
   };




   private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
       @Override
       public boolean onScale(ScaleGestureDetector detector) {
           scaleFactor *= detector.getScaleFactor();
           scaleFactor = Math.max(MIN_ZOOM, Math.min(scaleFactor, MAX_ZOOM));
           return true;
       }
   }


   public void executeClick (float x, float y) {

           if (lb1Rect.contains(x, y)) {
               lb1on = !lb1on;
               //new HttpAsyncTask().execute("http://10.30.60.190/arduino");

               invalidate();
           }
   }

}

看起来像

  private OnLongClickListener vLong = new OnLongClickListener() {

          @Override
          public boolean onLongClick(View view) {
              showContextMenu();
              lb1on = !lb1on;
              return true;
          }
      };

方法不起作用,我把lb1on =!lb1on;在那里检查灯泡图标是否会改变但没有任何反应。 这是我的活动,我在其中提出了观点:

公共类缩放扩展活动{

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);



       setContentView(R.layout.activity_zoom);
       ZoomView zoom = (ZoomView) findViewById(R.id.zoomView);



       registerForContextMenu(zoom);
   }

   @Override
   public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

       MenuInflater inflater = getMenuInflater();
       inflater.inflate(R.menu.zoom_context, menu);
       super.onCreateContextMenu(menu, v, menuInfo);
   }


   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       // Inflate the menu; this adds items to the action bar if it is present.
       getMenuInflater().inflate(R.menu.zoom, menu);
       return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
       // Handle action bar item clicks here. The action bar will
       // automatically handle clicks on the Home/Up button, so long
       // as you specify a parent activity in AndroidManifest.xml.
       int id = item.getItemId();
       if (id == R.id.action_settings) {
           return true;
       }
       return super.onOptionsItemSelected(item);
   }
  }

有可能吗?或者我可以用另一种方式创建一个这样的图形界面,可以看到整个楼层,可以放大和平移,还可以放置按钮?

0 个答案:

没有答案