As a result of executing the code, as you move your finger across the screen, Canva redraws the Bitmap with new coordinates. The inheritance from View.OnTouchListener was not and instead of onTouch was onTouchEvent. But I wanted the Ontouch listener to hang up on the ImageView. But as you can see, instead of the loath, I see setContentView (new GraphicsView (this)). So what is this new GraphicsView (this)? I understand that this is a GraphicsView object that inherits from View, but then what does it inherit from View? Why is it full screen and why is the background color black? where do these parameters come from? How do I hang an ImageView on it that I assign to an OnTouch listener?

public class MainActivity extends Activity { float touchX = 10,touchY= 10; private float korX = touchX, korY = touchY, initX=0, initY=0; ImageView ar; Bitmap bitmap; Canvas canvas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new GraphicsView(this)); ar.setImageResource(R.drawable.areatouch); ar = (ImageView) this.findViewById(R.id.imageView); } public class GraphicsView extends View implements View.OnTouchListener{ public GraphicsView(Context context) { super(context); ar.setOnTouchListener(this); } @Override protected void onDraw(Canvas canvas) { Bitmap mybitmap = BitmapFactory.decodeResource(getResources(),R.drawable.face_box); canvas.drawBitmap(mybitmap, touchX, touchY, null); } public boolean onTouch(View v,MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: korX = touchX; korY = touchY; initX = event.getRawX(); initY = event.getRawY(); return true; case MotionEvent.ACTION_MOVE: touchX = korX + (event.getRawX() - initX); touchY = korY + (event.getRawY() - initY); invalidate(); return true; } return false; } } } 

Closed due to the fact that it is necessary to reformulate the question so that it was possible to give an objectively correct answer by the participants Yuriy SPb , aleksandr barakin , user194374, D-side , Streletz 13 Jul '16 at 13:19 .

The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Comments are not intended for extended discussion; conversation moved to chat . - PashaPash

1 answer 1

You define only one context context. To create it through leyat, you need to define the remaining 2 constructors. In general, it is not very clear what you are trying to achieve - why not inherit from image view?

  • Please explain where I define one constructor with a context and which others I have not defined? On my canvas canvas draws bitmap and how the coordinates change redraw it in the new coordinates. I get new coordinates with Image in the form of a square at the bottom of the screen. OnTouch method should work only in the area of ​​this Image. - Turalllb 6:26 pm
  • First, define all three constructors for the GraphicsView `public GraphicsView (Context context) {super (context); } `` public GraphicsView (Context context, AttributeSet attrs) {super (context, attrs); } ` public GraphicsView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public GraphicsView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } - Alex Key
  • Schedule a view into <com.example.alkurop.myapplication.GraphicsView android:id="@+id/graphicsView" android:layout_width="match_parent" android:layout_height="match_parent"/> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent"/> - alex key
  • Then put on the same layout your image view. Pass this layout to setContentView(R.layout.activity_main); View imageView = findViewById(R.id.imageView); GraphicsView graphicsView = ((GraphicsView) findViewById(R.id.graphicsView)); imageView.setOnTouchListener(graphicsView); setContentView(R.layout.activity_main); View imageView = findViewById(R.id.imageView); GraphicsView graphicsView = ((GraphicsView) findViewById(R.id.graphicsView)); imageView.setOnTouchListener(graphicsView); - Alex Key