2015-03-31 64 views
0

我試圖做出像這樣的塗料塗覆: user inter face如何繪製表面視圖?

因此當用戶把他的手在表面視圖中,用戶將可以繪製面視圖內線路。 XML:

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

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Paint" 
    android:layout_marginTop="10sp" 
    android:layout_marginLeft="5sp" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:layout_weight="3" /> 
<TextView 
     android:id="@+id/textV" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="20dip" 
     android:layout_toRightOf="@+id/ivImage" 
     android:text="User Name" 

     /> 
     <ImageView 
     android:id="@+id/ivImage" 
     android:layout_width="50dp" 
     android:layout_height="match_parent" 
     android:src="@drawable/person" 
     /> 


     </LinearLayout> 

     <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:layout_marginTop="20sp"> 

     <ScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TextView 
     android:id="@+id/textView2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Assets Models" 
     android:gravity="center"/> 

     <Button 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:drawableLeft="@drawable/ic_launcher" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

     </LinearLayout> 
     </ScrollView> 

     <SurfaceView 
     android:id="@+id/surfaceView1" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="2" /> 

     <ScrollView 
     android:id="@+id/scrollView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:layout_gravity="right" > 

     <TextView 
     android:id="@+id/textView3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="Assets Models" /> 

     <Button 
     android:id="@+id/button2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:drawableLeft="@drawable/ic_launcher" 
     android:text="Button" /> 

     </LinearLayout> 
     </ScrollView> 
     </LinearLayout> 



     </LinearLayout> 

沒有人知道如何與surfaceview或者是在XML文件中定義不是在一個動態的方式,所以我可以把那些多餘的元素,例如按鈕的視圖,滾動,用戶名做和圖像在同一個活動。 我真的很感謝任何幫助。 謝謝

回答

1

要管理它,你可以按照以下步驟。

  1. 創建一個java類並使用SurfaceView對其進行擴展。

  2. 使用下面的代碼

  3. 和使用,作爲UI組件在你的 「yourlayout.xml」

    public class mySurfaceView extends SurfaceView { 
    
        public static Paint mPaint; 
        public static Path path; 
        public static Bitmap mBitmap; 
        public static Canvas mCanvas; 
        private ArrayList<PathWithPaint> _graphics1 = new ArrayList<PathWithPaint>(); 
    
        public mySurfaceView(Context context, AttributeSet attrs) { 
        super(context, attrs); 
        path = new Path(); 
        mBitmap = Bitmap.createBitmap(820, 480, Bitmap.Config.ARGB_8888); 
        mCanvas = new Canvas(mBitmap); 
        this.setBackgroundColor(Color.BLACK); 
        mPaint = new Paint(); 
        mPaint.setDither(true); 
        mPaint.setColor(0xFFFFFF00); 
        mPaint.setStyle(Paint.Style.STROKE); 
        mPaint.setStrokeJoin(Paint.Join.ROUND); 
        mPaint.setStrokeCap(Paint.Cap.ROUND); 
        mPaint.setStrokeWidth(3); 
        } 
    
        @Override 
        public boolean onTouchEvent(MotionEvent event) { 
        PathWithPaint pp = new PathWithPaint(); 
        mCanvas.drawPath(path, mPaint); 
        if (event.getAction() == MotionEvent.ACTION_DOWN) { 
         path.moveTo(event.getX(), event.getY()); 
         path.lineTo(event.getX(), event.getY()); 
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) { 
         path.lineTo(event.getX(), event.getY()); 
         pp.setPath(path); 
         pp.setmPaint(mPaint); 
         _graphics1.add(pp); 
        } 
        invalidate(); 
        return true; 
        } 
    
        @Override 
        protected void onDraw(Canvas canvas) { 
        super.onDraw(canvas); 
        if (_graphics1.size() > 0) { 
         canvas.drawPath(_graphics1.get(_graphics1.size() - 1).getPath(), 
          _graphics1.get(_graphics1.size() - 1).getmPaint()); 
        } 
        } 
    
        public class PathWithPaint { 
        private Path path; 
        public Path getPath() { 
         return path; 
        } 
        public void setPath(Path path) { 
         this.path = path; 
        } 
        private Paint mPaint; 
        public Paint getmPaint() { 
         return mPaint; 
        } 
        public void setmPaint(Paint mPaint) { 
         this.mPaint = mPaint; 
        } 
        } 
    } 
    

,現在我只想用它在我的佈局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin"  tools:context=".MainActivity"> 

<ayttunc.example.com.drawingstackoverflow.mySurfaceView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

</RelativeLayout> 

然後你只是運行它,但我希望你注意到我們沒有在MainA中寫任何代碼活動,並且在你運行它之後,你會看到類似的東西!

http://i.stack.imgur.com/YjGk6.png

如果你想了解更多信息,您可以通過 [email protected]給我發郵件。

+0

FWIW,您的代碼似乎是在SurfaceView的視圖上繪製的,而不是SurfaceView的表面。您最好使用自定義視圖,因爲這會被硬件加速。 http://developer.android.com/training/custom-views/index.html – fadden 2015-04-01 21:52:46