2012-04-24 136 views
4

我是Android编程的新手。我的应用程序是来自开发人员android网站上的api演示的示例应用程序。当我在该示例绘图中更改参数时,它会变大。该图需要在滚动视图中显示(不需要缩小以适应屏幕)。这是我使用的代码: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 
     } 
    } 
} 

这里的XML代码:

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> 

当我运行该应用程序,按下主活动应用程序崩溃中的按钮。 图纸看起来像如图一的时候我不使用XML布局或scrollviews:

http://i.stack.imgur.com/za5MP.png 如图一

我也尝试过使用这种方法的setContentView此代码后:

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

而且这一个:

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

大家好,没有人公布任何回答我的问题,所以我找了一些解决方案。我发现在Android没有实现双向滚动 我发现真的很好的双向滚动这个网页上的实现[链接] http://stackoverflow.com/questions/2044775/scrollview-vertical-and-horizo​​ntal-in-解决方案由Mahdi Hijazi发布。解决方案就像魅力一样,但是当我尝试在他的解决方案上实现我的SampleView类时,再也没有任何反应。如果我得到了一些解决方案,我会在这里发布解决方案。直到那时希望有人有解决办法。 – Kristijan 2012-05-05 19:05:02

回答

4

终于我找到了解决我的问题。我用的解决方案,从这个网站:

Link1

好,我之前看到的解决方案,但不幸的是我没有意识到,这种解决方案是为我好。我知道我需要用Canvas的onMeasure方法来显示它在xml中,但没有从提到的网站的解决方案它不起作用。现在它可以工作。这是我的xml和SampleView解决方案。

XML代码:

<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> 

这是我SampleView解决方案:

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 2014-04-02 11:45:27

10

写这条线在自定义视图类的TouchEvent

的getParent()。requestDisallowInterceptTouchEvent(真)

+1

这真是太棒了:)最好的解决方案,谢谢! 我只是在'ScrollView'父对象上调用它。 – JWqvist 2014-01-20 10:45:00