2014-07-14 14 views
0

我想創建兩個視圖,其中左側視圖應該包含圖像,而右側視圖應該擴展用於在繪製方法中創建點的運行時視圖。在Android中創建TwoView

的FrontPage ::

ActivityB.java

public class ActivityB extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(new EventViewB(this, null)); 
    } 

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

} 

運行時查看內容:

EventViewB.java

public class EventViewB extends View { 

    private Paint paint = new Paint(); 
    private Path path = new Path(); 
    int c1=0,c2=0,c3=0,c4=0,c5=0,c6=0,c7=0,c8=0,c9=0,c10=0,c11=0,c12=0,c13=0,c14=0,c15=0; 
    int counterIncrement=0; 

public EventViewB(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    paint.setAntiAlias(true); 
    paint.setStrokeWidth(10f); 
    paint.setColor(Color.BLACK); 
    paint.setStyle(Paint.Style.STROKE); 
    paint.setStrokeJoin(Paint.Join.ROUND); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    canvas.drawPath(path, paint); 
    canvas.drawPoint(200, 100, paint); 
    canvas.drawPoint(200, 150, paint); 
    canvas.drawPoint(200, 200, paint); 
    canvas.drawPoint(200, 250, paint); 
    canvas.drawPoint(200, 300, paint); 
    canvas.drawPoint(200, 350, paint); 
    canvas.drawPoint(200, 400, paint); 
    // first curve 
    canvas.drawPoint(250, 100, paint); 
    canvas.drawPoint(300, 100, paint); 
    canvas.drawPoint(340, 140, paint); 
    canvas.drawPoint(360, 175, paint); 
    canvas.drawPoint(340, 210, paint); 
    canvas.drawPoint(300, 250, paint); 
    canvas.drawPoint(250, 250, paint); 

    // Second Curve 
    canvas.drawPoint(340, 290, paint); 

    canvas.drawPoint(360, 325, paint); 
    canvas.drawPoint(340, 360, paint); 
    canvas.drawPoint(300, 400, paint); 
    canvas.drawPoint(250, 400, paint); 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    float eventX = event.getX(); 
    float eventY = event.getY(); 


    switch (event.getAction()) { 
    case MotionEvent.ACTION_DOWN: 
     path.moveTo(200,100); 
     return true; 

    case MotionEvent.ACTION_MOVE: 
     if(eventX>195 && eventX<202) 
      if(eventY>100 && eventY<150) 
       if(c1==0) 
       { 
       path.lineTo(200, 150); 
       counterIncrement++; 
       c1++; 
       } 
     if(eventX>195 && eventX<202) 
      if(eventY>150 && eventY<200) 
       if(c2==0 && counterIncrement==1) 
       { 
       path.lineTo(200, 200); 
       counterIncrement++; 
       c2++; 
       } 

     if(eventX>195 && eventX<202) 
      if(eventY>200 && eventY<250) 
       if(c3==0 && counterIncrement==2) 
       { 
       path.lineTo(200, 250); 
       counterIncrement++; 
       c3++; 
       } 
     if(eventX>195 && eventX<202) 
      if(eventY>250 && eventY<300) 
       if(c4==0 && counterIncrement==3) 
       { 
       path.lineTo(200, 300); 
       counterIncrement++; 
       c4++; 
       } 
     if(eventX>195 && eventX<202) 
      if(eventY>300 && eventY<350) 
       if(c5==0 && counterIncrement==4) 
       { 
       path.lineTo(200, 350); 
       counterIncrement++; 
       c5++; 
       } 

     if(eventX>195 && eventX<202) 
      if(eventY>350 && eventY<400) 
       if(c6==0 && counterIncrement==5) 
       { 
       path.lineTo(200, 400); 
       counterIncrement++; 
       c6++; 
       } 

     // first Curve 
     if(eventX>200 && eventX<250) 
      if(eventY>99 && eventY<101) 
       if(c7==0 && counterIncrement==6) 
       { 
       path.moveTo(200, 100); 
       path.lineTo(250, 100); 
       counterIncrement++; 
       c7++; 
       } 

     if(eventX>250 && eventX<300) 
      if(eventY>99 && eventY<101) 
       if(c8==0 && counterIncrement==7) 
       { 
       path.moveTo(250, 100); 
       path.lineTo(300, 100); 
       counterIncrement++; 
       c8++; 
       } 
     if(eventX>300 && eventX<340) 
      if(eventY>100 && eventY<140) 
       if(c9==0 && counterIncrement==8) 
       { 
       path.moveTo(300, 100); 
       path.lineTo(340, 140); 
       counterIncrement++; 
       c9++; 
       } 
     if(eventX>340 && eventX<360) 
      if(eventY>140 && eventY<175) 
       if(c10==0 && counterIncrement==9) 
       { 
        path.moveTo(340, 140); 
        // 
       path.lineTo(360, 175); 
       counterIncrement++; 
       c10++; 
       } 
     if(eventX>340 && eventX<360) 
      if(eventY>175 && eventY<210) 
       if(c11==0 && counterIncrement==10) 
       { 
        path.moveTo(360, 175); 
       path.lineTo(340,210); 
       counterIncrement++; 
       c11++; 
       } 

     if(eventX>300 && eventX<340) 
      if(eventY>210 && eventY<250) 
       if(c12==0 && counterIncrement==11) 
       { 
        path.moveTo(340, 210); 
       path.lineTo(300,250); 
       counterIncrement++; 
       c12++; 
       } 

     if(eventX>250 && eventX<300) 
      if(eventY>249 && eventY<251) 
       if(c13==0 && counterIncrement==12) 
       { 
        path.moveTo(300, 250); 
       path.lineTo(250,250); 
       counterIncrement++; 
       c13++; 
       } 
     if(eventX>200 && eventX<250) 
      if(eventY>249 && eventY<251) 
       if(c14==0 && counterIncrement==13) 
       { 
        path.moveTo(250, 250); 
       path.lineTo(200,250); 
       counterIncrement++; 
       c14++; 
       } 

     if(eventX>300 && eventX<340) 
      if(eventY>250 && eventY<290) 
       if(c15==0 && counterIncrement==14) 
       { 
        path.moveTo(300, 250); 
       path.lineTo(340,290); 

       counterIncrement++; 
       c15++; 
       } 



     break; 
    case MotionEvent.ACTION_UP: 
     path.moveTo(250,400); 
     break; 
    default: 
     return false; 
    } 
    invalidate(); 
    return true; 

    } 
} 

我必須要按一下按鈕,打開運行系統視圖信息它們打算打開上面顯示的另一個活動類..現在我想在兩個視圖中做到這一點,一個字母和視圖的視圖包含此運行時視圖?

+0

尋找碎片? – nobalG

+0

你可以把你的代碼? – KOTIOS

+0

嗨,我想創建兩個視圖,一個用於加載Alphabet的圖像。我在運行時視圖中已經有了一些內容,並且此運行時視圖應該放置在第二個視圖中....但是現在,我已經爲運行時視圖中顯示的信息編寫了代碼,因此現在我添加了兩個視圖,一個爲此和其他圖像。 – Ashok

回答

0

您需要創建兩個視圖,該視圖的一半運行時間視圖和另一半你需要一個圖像,你想顯示它..這是你的闕。 RIT ..?

我也是新來的android,我搜索了一些延伸。我的解決方案不是創建新視圖,而是爲什麼不在現有畫布本身中添加圖像。我提到這個Drawable image on a canvas鏈接顯示瞭如何在畫布視圖中添加圖像,並且我嘗試了它爲我工作。

這是我添加到畫布中顯示圖像的代碼(藉助該鏈接)。

//ADD THIS PIECE OF CODE UNDER THE CLASS WHICH EXTENDS VIEW 
Resources res = getResources(); 
Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.yourimage); 
//Set the height of the image 
private int dstHeight=100; 
//Set the width of the image 
private int dstWidth=100; 
private boolean filter; 
Bitmap mBitmap = Bitmap.createScaledBitmap(bitmap, dstWidth, dstHeight, filter); 

//"ADD THIS LINE OF CODE IN UR DRAW METHID" 
//10 & 200 represent ur image placement in X , Y axis 
canvas.drawBitmap(mBitmap, 10, 200, mBitmapPaint); 

希望這段代碼將圖像添加到你的畫布中。 如果我有任何錯誤,請原諒我。謝謝:)

0

使用片段。 (有點像主詳細表格)

您可以在Java中聲明一個片段是這樣的(這將走在你的MainActivity類):

Fragment newFragment = new FragStart(); // the name of the class for the fragment, not the current class 
    FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
    transaction.replace(R.id.mainFragment, newFragment); 
    transaction.addToBackStack(null); 
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
    transaction.commit(); 

然後你FragStart()類是這樣的:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.frag_start, container, false); 
} 

和你frag_start佈局可能是你在它想要的東西,但你的主要佈局(爲MainActivity應該是這樣的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal" > 

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:layout_weight="1" 
    android:background="#777777" 
    android:weightSum="3" > 

    <Button 
     android:id="@+id/btnDiary" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Diary" 
     android:background="#777777" 
     android:textColor="#FFFFFF" /> 

    <Button 
     android:id="@+id/btnPatients" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Patients" 
     android:background="#777777" 
     android:textColor="#FFFFFF" /> 

    <Button 
     android:id="@+id/btnTools" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Tools" 
     android:background="#777777" 
     android:textColor="#FFFFFF" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/mainFragment" 
    android:layout_weight="3" 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
</LinearLayout> 

的片段佈局將是:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="This is the fragment" 
    android:textSize="40sp" /> 

</RelativeLayout> 

給你以下結果:

image

+0

謝謝,,這應該只能在碎片啊..或者其他? – Ashok

+0

@ user3208981我只會在片段中做到這一點,我只做了它的片段 –

+0

你只需點擊左邊的按鈕,它應該顯示在右邊的內容,但我想知道點擊按鈕的時間在上一頁或活動中,它將在單個視圖中加載兩個視圖,左側應該包含字母表,並且右側應該是運行時視圖 – Ashok