Invalidate不执行onDraw方法

时间:2015-11-02 15:26:29

标签: java android

我已经在这里检查了关于onDraw方法的主题,但我仍然无法解决我的问题。我必须使用类 - 一个扩展AppCompatActivity的Map.class和另一个扩展View.class的MapPoint.class。在我调用invalidate后,没有继续。

public class Map extends AppCompatActivity {

ImageView imageView2;
Button button7;
Button button8;
String file;
public static int count=0;
Paint paint = new Paint();
float x;
float y;
RectF oval;
MapPoint point;


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


    button7 = (Button) findViewById(R.id.button7);   // Zaznacz gdzie jesteś
    button8 = (Button) findViewById(R.id.button8);  //Zlokalizuj mnie
    imageView2 = (ImageView) findViewById(R.id.imageView2);

    final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/compareFolder/";
    File newdir = new File(dir);
    newdir.mkdirs();

    button8.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            count++;
            file = dir + count + ".jpg";
            File newfile = new File(file);
            try {
                newfile.createNewFile();
            } catch (IOException e) {
            }

            Uri outputFileUri = Uri.fromFile(newfile);

            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, 1);
            Toast.makeText(getApplicationContext(),"Zrób zdjęcie charakterystycznego przedmiotu który znajduje się w Twoim sąsiedztwie", Toast.LENGTH_LONG).show();

        }
    });

    button7.setOnClickListener(new View.OnClickListener() {  //zaznacz gdzie jestes
        @Override
        public void onClick(View view) {
            point = new MapPoint(getApplicationContext());

           point.invalidate();

        }
    });

}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1 && resultCode == RESULT_OK) {
        Log.d("CameraDemo", "Pic saved");
        Bitmap btm = (Bitmap)data.getExtras().get("data");

    }
}

@Override
public void onBackPressed() {
    finish();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, 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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

MapPoint.clas在哪里是onDraw方法

public class MapPoint extends View {


float x;
float y;
RectF oval;
RectF oval2;
Paint paint = new Paint();
Map map = new Map();


public MapPoint(Context context) {

    super(context);
    setWillNotDraw(false);

}


@Override
protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);  
        paint.setColor(Color.RED);
        canvas.drawRect(5, 5, 6, 6, paint);

}

protected void drawDestination(float x, float y) {

    oval2 = new RectF(x - 30, y - 30, x + 30, y + 30);

}

}

我会非常感激任何帮助,因为我不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

我并没有完全理解你的问题是什么但是在检查了我的一个扩展View并重写onDraw()的类后,我注意到了很大的差异。多数民众赞成我如何画一些东西,也许它可以帮助你:

@Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.save();

        //draw something

        canvas.restore();
    }