2012-12-07 43 views
0

所以,我創建了一個在畫布上繪製三角形的android活動。我還向虛擬機添加了4個菜單(顏色,放大,縮小和重置)。顏色工作正常,但我不太清楚在按下菜單按鈕時如何在android中調整三角形的大小。該任務表示只是修復三角形的頂點,然後更改底部兩點的座標三角形。任何人都可以指出我在正確的方向如何在Android中做到這一點?設置三角形的大小(Android)

這裏是我的代碼,雖然放大,縮小和重置的實現設置爲使用圓形(我之前做過的項目),而不是三角形。請注意,「顏色」菜單的作品,所以沒有必要這樣做。

public class MainActivity extends Activity 
{ 
    final Context context = this; 
    private Graphics graphic; 
    private Dialog radiusDialog; //Creates dialog box declaration 
    private SeekBar red; 
    private SeekBar green; 
    private SeekBar blue; 
    private Button radiusButton; 

    private TextView progress1; 
    private TextView progress2; 
    private TextView progress3; 
    private TextView tv; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState);  
     graphic = new Graphics(this); //Create new instance of graphics view 
     setContentView(graphic); //Associates customized view with current screen  
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) //This acts as a menu listener to override 
    { 
     switch(item.getItemId()) //returns menu item 
     { 
     case R.id.Color: 
      showDialog(); 
      break; 
     case R.id.Shrink: 
      graphic.setRadius(graphic.getRadius() -1); 
      graphic.invalidate(); 
      break; 
     case R.id.Enlarge: 
      graphic.setRadius(graphic.getRadius() +1); 
      graphic.invalidate(); 
      break; 
     case R.id.Reset: 
      graphic.setColor(Color.CYAN); 
      graphic.setRadius(75); 
      graphic.invalidate(); 
      break; 
     } 
     return super.onOptionsItemSelected(item); 
    } 





    void showDialog() //creates memory for dialog 
    { 
     radiusDialog = new Dialog(context); 
     radiusDialog.setContentView(R.layout.draw_layout); //binds layout file (radius) with current dialog 
     radiusDialog.setTitle("Select Color:"); 


     red = (SeekBar)radiusDialog.findViewById(R.id.seekBar1); 
     green = (SeekBar)radiusDialog.findViewById(R.id.seekBar2); 
     blue = (SeekBar)radiusDialog.findViewById(R.id.seekBar3); 

     progress1 = (TextView)radiusDialog.findViewById(R.id.textView2); 
     progress2 = (TextView)radiusDialog.findViewById(R.id.textView4); 
     progress3 = (TextView)radiusDialog.findViewById(R.id.textView6);  

     mychange redC = new mychange(); 
     red.setOnSeekBarChangeListener(redC); 

     mychange greenC = new mychange(); 
     green.setOnSeekBarChangeListener(greenC); 

     tv = (TextView)radiusDialog.findViewById(R.id.textView7); 
     mychange c = new mychange(); 
     blue.setOnSeekBarChangeListener(c);  
     radiusButton = (Button) radiusDialog.findViewById(R.id.button1); 
     radiusButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       int color = Color.rgb(red.getProgress(), green.getProgress(), blue.getProgress()); 
       radiusDialog.dismiss(); 
       setContentView(R.layout.activity_main); 
       setContentView(graphic); 
       graphic.setColor(color);//Create new instance of graphics view 
       graphic.invalidate(); 
      } 
     }); 
     radiusDialog.show(); //shows dialog on screen 
    } 

    public class mychange implements OnSeekBarChangeListener{ 

     @Override 
     public void onProgressChanged(SeekBar seekBar, int progress, 
       boolean fromUser) { 
      // TODO Auto-generated method stub 
      int color = Color.rgb(red.getProgress(), green.getProgress(), blue.getProgress()); 
      tv.setBackgroundColor(color); 
      progress1.setText(String.valueOf(red.getProgress())); 
      progress2.setText(String.valueOf(green.getProgress())); 
      progress3.setText(String.valueOf(blue.getProgress())); 

     } 

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

     } 

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

     } 
    } 

} 

Graphics類繪製三角形

public class Graphics extends View 
{ 

    private Paint paint; 
    private int radius; 
    private int color; 

    public void setColor(int color) 
    { 
     this.color = color; 
    } 

    public Graphics(Context context) //creates custom view (constructor) 
    { 
     super(context); 
     paint = new Paint(); //create instance of paint 
     color = Color.CYAN; 
     paint.setStyle(Paint.Style.FILL); //draw filled shape 
     radius = 75; 
    } 

    @Override 
    protected void onDraw(Canvas canvas) //override onDraw method 
    { 
     super.onDraw(canvas); 
     paint.setColor(color); 
     paint.setStyle(Paint.Style.STROKE); 
     Path path = new Path(); 
     path.moveTo(230, 200); 
     path.lineTo(330, 300); 
     path.lineTo(130, 300); 
     path.close(); 
     canvas.drawPath(path, paint); 
    } 

    void setRadius(int radius) 
    { 
     this.radius = radius; 
     invalidate(); //just like repaint method 
    } 
    public int getRadius() 
    { 
     return radius; 
    } 
} 

回答

0

如果頂部座標保持固定,您可以更改三角形的高度縮小/放大。

可以說三角形是等邊的 - 所有3邊都有相同的長度。在這種情況下:

enter image description here

因此,如果頂的頂點座標爲(X,Y),底部的座標將是:

(x - side/2, y + h) 

和:

(x + side/2, y + h) 

所以你路徑代碼應寫爲:

float side = Math.sqrt(3)/2 * height; 
Path path = new Path(); 
path.moveTo(x, y); 
path.lineTo(x - side/2, y + height); 
path.lineTo(x + side/2, y + height); 
path.close(); 
+0

有趣的我會努力工作。 Ty:D – user1780149