2012-04-24 8 views
4

나는 Android 용 프로그래밍에있어서 꽤 새로운 기술이다. 내 앱은 개발자 Android 웹 사이트의 api 데모에서 제공하는 샘플 앱입니다. 그 샘플 드로잉에서 매개 변수를 변경하면 더 커집니다. 이 그림은 스크롤 뷰로 표시해야합니다 (화면에 맞게 축소 할 필요가 없습니다). XML 코드Android : Scrollview의 캔버스에 그리기

DrawPoints.java 여기

public class DrawPoints extends myActivity { 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.routes); 

    } 

    public static class SampleView extends View { 
     private Paint mPaint = new Paint(); 
     private float[] mPts; 

     /*here comes declaration of parametars 



     private void buildPoints() { 

     /*here comes some coding*/ 

      } 
     } 

     public SampleView(Context context, AttributeSet attributeset) { 
      super(context, attributeSet); 

      buildPoints(); 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
      Paint paint = mPaint; 

      //here also comes code 
     } 
    } 
} 

된다 : 이것은 내가 사용하는 코드입니다

routes.xml

<?xml version="1.0" encoding="utf-8"?> 
    <ScrollView 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/scrollView1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" > 
     <HorizontalScrollView 
       android:id="@+id/scrollView2" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 

        <!-- This code is just to make sure that scroll views work how 
         I want them to work, image size is 625*351 px 
        <ImageView 
         android:id="@+id/image_View1" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:src="@drawable/bus_design" 
          /> --> 
        <my.package.DrawPoints.SampleView 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" /> 


     </HorizontalScrollView> 
    </ScrollView> 

내가 그 응용 프로그램을 실행하고 주요 활동 응용 프로그램 충돌시 버튼을 누릅니다. 이

View v = new SampleView(this); 
addContentView(v, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
      ViewGroup.LayoutParams.FILL_PARENT)); 

과 : 나는 또한 메소드 된 setContentView 후이 코드를 사용하려고

http://i.stack.imgur.com/za5MP.png 그림 1 : 나는 XML 레이아웃 또는 scrollviews을 사용하지 않는 경우 도면은 그림 1과 같다 하나 :

View v = new SampleView(this); 
    ScrollView.LayoutParams lp = new ScrollView.LayoutParams(1000, 1000); 

    addContentView(v, lp); 

내가 co des, 위의 그림에서 스크롤 뷰없이 그림 1을 표시하면 두 번째 내용보기가 XML을 덮어 쓰지만 올바르게 표시되지 않는 것처럼 보입니다. 는 그 후 나는 된 setContentView 후이 코드를 사용하려고 :

View v = new SampleView(this); 
    FrameLayout fl = new FrameLayout(this); 
    fl.findViewById(R.id.FrameLayout1); 
    fl.addView(v); 

프레임 레이아웃 (FrameLayout1가) 수평 스크롤 뷰 후 routes.xml 파일에 추가됩니다. 응용 프로그램이 실행되면 그림 1이없는 빈 화면이 표시됩니다. 누구나 ScrollView에서 그림 1을 표시하는 코드를 업그레이드하는 방법을 알고 있습니까?

미리 감사드립니다.

+0

안녕하세요 모든 사람이 아무도 내 질문에 어떤 대답을 게시하지 커스텀 뷰 클래스의 당신의 touchevent

의 getParent(). requestDisallowInterceptTouchEvent (참)이 라인은 그래서 몇 가지 솔루션을 보였다. 나는 안드로이드에서 양방향 스크롤링을 구현하지 않았 음을 발견했습니다 나는이 웹 페이지에서 양방향 스크롤링의 실제적인 구현을 발견했습니다 [link] http://stackoverflow.com/questions/2044775/scrollview-vertical-and-horizontal-in- 안드로이드 솔루션은 Mahdi Hijazi에 의해 게시되었습니다. 솔루션은 매력처럼 작동하지만, 다시 내 솔루션에 내 SampleView 클래스를 구현하려고하면 아무 일도 일어나지 않습니다. 솔루션을 얻은 경우 여기에 해당 솔루션을 게시 할 것입니다. 그때까지 누군가가 해결책을 가지기를 바랍니다. – Kristijan

답변

4

마지막으로 내 문제에 대한 해결책을 찾았습니다.

Link1

잘 내가 전에 해당 솔루션을 보았다,하지만 불행히도 나는이 솔루션은 나를 위해 좋은 몰랐어요 :이 사이트에서 솔루션을 사용했다. Canvas가 xml로 보여주기 위해 Canvas에 onMeasure 메소드가 필요하다는 것을 알았지 만, 언급 된 사이트의 해결책이 없기 때문에 작동하지 않았습니다. 이제 작동합니다. 여기 내 XML 및 SampleView 솔루션입니다.

XML 코드 : 이것은 내 SampleView 솔루션

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/app_layout" android:orientation="horizontal" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <!-- SCENE --> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/scene_layout" 
     android:drawingCacheQuality="low" 
     android:orientation="horizontal" android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <com.testiranje.Kristijan.TwoDScrollView 
      android:id="@+id/scene_scroller" android:drawingCacheQuality="low" 
      android:scrollbars="horizontal" 

      android:layout_width="fill_parent" android:layout_height="fill_parent"> 
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/scene_container" 
       android:drawingCacheQuality="low" 
       android:background="@drawable/map" 
       android:layout_width="fill_parent" android:layout_height="fill_parent"> 

       <com.testiranje.Kristijan.SampleView 
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent"> 

        </com.testiranje.Kristijan.SampleView> 

       <!-- <ImageView xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/scene_background" android:drawingCacheQuality="low" 
        android:background="@drawable/map" 
        android:layout_width="fill_parent" android:layout_height="fill_parent" /> --> 
      </RelativeLayout> 
     </com.testiranje.Kristijan.TwoDScrollView> 
    </RelativeLayout> 
</RelativeLayout> 

입니다 : 이제

public class SampleView extends View { 
    private Paint mPaint = new Paint(); 
    private float[] mPts; 

    private static final float SIZE = 1000; 
    private static final int SEGS = 50; 
    private static final int X = 0; 
    private static final int Y = 1; 

    private void buildPoints() { 
     final int ptCount = (SEGS + 1) * 2; 
     mPts = new float[ptCount * 2]; 

     float value = 0; 
     final float delta = SIZE/SEGS; 
     for (int i = 0; i <= SEGS; i++) { 
      mPts[i*4 + X] = SIZE - value; 
      mPts[i*4 + Y] = 0; 
      mPts[i*4 + X + 2] = 0; 
      mPts[i*4 + Y + 2] = value; 
      value += delta; 
     } 
    } 

    public SampleView(Context context){ 
     super(context); 
     //initSampleView(); 
     buildPoints(); 

    } 

    //This constructor is very important because withouth of this 
    //you can't insert this view in xml 
    public SampleView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     //initSampleView(); 
     buildPoints(); 
    } 

    /*private final void initSampleView() { 
     mPaint = new Paint(); 
     mPaint.setAntiAlias(true); 
     setPadding(3, 3, 3, 3); 
    }*/ 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     setMeasuredDimension(measureWidth(widthMeasureSpec), 
       measureHeight(heightMeasureSpec)); 
    } 

    /** 
    * Determines the width of this view 
    * @param measureSpec A measureSpec packed into an int 
    * @return The width of the view, honoring constraints from measureSpec 
    */ 
    private int measureWidth(int measureSpec) { 
     int result = 0; 
     //This is because of background image in relativeLayout, which is 1000*1000px 
     measureSpec = 1001; 
     int specMode = MeasureSpec.getMode(measureSpec); 
     int specSize = MeasureSpec.getSize(measureSpec); 

     if (specMode == MeasureSpec.UNSPECIFIED) { 
      // We were told how big to be 
      result = specSize; 
     } 

     return result; 
    } 

    /** 
    * Determines the height of this view 
    * @param measureSpec A measureSpec packed into an int 
    * @return The height of the view, honoring constraints from measureSpec 
    */ 
    private int measureHeight(int measureSpec) { 
     int result = 0; 
     //This is because of background image in relativeLayout, which is 1000*1000px 
     measureSpec = 1001; 
     int specMode = MeasureSpec.getMode(measureSpec); 
     int specSize = MeasureSpec.getSize(measureSpec); 


     if (specMode == MeasureSpec.UNSPECIFIED) { 
      // Here we say how Heigh to be 
      result = specSize; 
     } 
     return result; 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     Paint paint = mPaint; 

     canvas.translate(10, 10); 

     paint.setColor(Color.RED); 
     paint.setStrokeWidth(0); 
     canvas.drawLines(mPts, paint); 


     paint.setColor(Color.BLUE); 
     paint.setStrokeWidth(3); 
     canvas.drawPoints(mPts, paint); 

    } 
} 

내가 가지고 이미지, 나는 내 응용 프로그램을 실행할 때 :

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

누구든지이 점에 대해 궁금한 점이 있으면 언제든지 물어보십시오. :).

+0

캔버스에 스크롤을 추가하는 방법은 무엇입니까? – AnAndroid

10

쓰기

+1

그건 너무 awsome :) 지금까지 최고의 솔루션으로 감사합니다! 그냥 'ScrollView' 부모에서 이것을 호출합니다. – JWqvist