2015-11-02 85 views
0

我已經檢查過關於onDraw方法的主題,但我仍然無法解決我的問題。我有類 - 一個擴展AppCompatActivity的Map.class和第二個MapPoint.class擴展View.class。在我稱之爲無效後,沒有發生。Invalidate不會執行onDraw方法

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); 
} 

}

在哪裏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); 

} 

}的MapPoint.clas

我會很感激的任何幫助,因爲我不知道該怎麼多做。

+1

你正在創建一個'MapPoint',它不添加任何容器'View'所以你怎麼能指望它來顯示? – pskink

+0

那麼我應該怎麼做才能將MapPoint添加到某個容器? – johansonik

回答

0

我並沒有完全明白你的問題是什麼,但是在檢查了一個擴展了View和覆蓋onDraw()的類後,我發現了很大的差異。那我怎麼畫點什麼,也許它可以幫助你:

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

     canvas.save(); 

     //draw something 

     canvas.restore(); 
    } 
+0

感謝您的幫助,但它不適合我。我的問題是,當我調用onDraw方法通過使其無效,它不繪製任何東西,該方法不能執行,所以我有問題。 – johansonik

相關問題