2012-06-10 23 views
0

我嘗試使用imageview滾動視圖完全像標識測驗應用程序中的滾動視圖。在JAVA代碼中添加ImageViews到滾動視圖

我創建了一個滾動視圖的XML文件。

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/beige" 
    > 
    <LinearLayout 
     android:id="@+id/layout_level1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
    </LinearLayout> 
</ScrollView> 

,比我寫的:

public class Level1 extends Activity { 

Level1Draw L1; 
LinearLayout l; 
ScrollView s; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    L1 = new Level1Draw(this); 
    setContentView(R.layout.level1); 


    l = (LinearLayout) findViewById(R.id.layout_level1); 


    l.addView(L1); 


} 

這是Level1Draw類:

private class Level1Draw extends View { 

     Bitmap[][] logo_1 = {{BitmapFactory.decodeResource(getResources(), R.drawable.cellcom),BitmapFactory.decodeResource(getResources(), R.drawable.channel1),BitmapFactory.decodeResource(getResources(), R.drawable.doctor_gav)}, 
       {BitmapFactory.decodeResource(getResources(), R.drawable.golfnew),BitmapFactory.decodeResource(getResources(), R.drawable.globes_only_logo_acrobat),BitmapFactory.decodeResource(getResources(), R.drawable.foxgroup3)}, 
       {BitmapFactory.decodeResource(getResources(), R.drawable.hando),BitmapFactory.decodeResource(getResources(), R.drawable.hafenix),BitmapFactory.decodeResource(getResources(), R.drawable.haaretz)}, 
       {BitmapFactory.decodeResource(getResources(), R.drawable.laisha),BitmapFactory.decodeResource(getResources(), R.drawable.jerusalempostred),BitmapFactory.decodeResource(getResources(), R.drawable.hop)}, 
       {BitmapFactory.decodeResource(getResources(), R.drawable.maariv),BitmapFactory.decodeResource(getResources(), R.drawable.logo),BitmapFactory.decodeResource(getResources(), R.drawable.logodelta)}, 
       {BitmapFactory.decodeResource(getResources(), R.drawable.renuar),BitmapFactory.decodeResource(getResources(), R.drawable.ravbariah),BitmapFactory.decodeResource(getResources(), R.drawable.pelephone)}, 
       {BitmapFactory.decodeResource(getResources(), R.drawable.shilav),BitmapFactory.decodeResource(getResources(), R.drawable.sano),BitmapFactory.decodeResource(getResources(), R.drawable.reshet_tv)}, 
       {BitmapFactory.decodeResource(getResources(), R.drawable.steimatzky),BitmapFactory.decodeResource(getResources(), R.drawable.srigamish),BitmapFactory.decodeResource(getResources(), R.drawable.sport5)}, 
       {BitmapFactory.decodeResource(getResources(), R.drawable.tambur),BitmapFactory.decodeResource(getResources(), R.drawable.supersal),BitmapFactory.decodeResource(getResources(), R.drawable.superpharm)}, 
       {BitmapFactory.decodeResource(getResources(), R.drawable.yediot),BitmapFactory.decodeResource(getResources(), R.drawable.walla),BitmapFactory.decodeResource(getResources(), R.drawable.tzometsfarim)}, 
       }; 

     public Level1Draw(Context context) { 
      // TODO Auto-generated constructor stub 
      super(context); 


     } 



     @Override 
     protected void onDraw(Canvas canvas) { 
      // TODO Auto-generated method stub 
      super.onDraw(canvas); 
      for(int i=0;i<10;i++){ 
       for(int j=0;j<3;j++){ 
      canvas.drawBitmap(logo_1[i][j], 60*(j+1)+80*j, 60*(i+1)+80*i, null); 
       } 
      } 

     } 




    } 

如果我在添加L1視圖時編寫了特定的寬度和高度,我可以在屏幕中看到視圖,但不能作爲滾動視圖,否則不起作用。

+0

我們需要一些更多的上下文;什麼是Level1Draw對象的寬度和高度設置爲?你是什​​麼意思,你可以在屏幕上看到它,但不是一個滾動視圖? – Blaskovicz

+0

我添加了Level1Draw類 – user1447655

回答

0

您的「Level1Draw」類沒有在其中實現onMeasure,因此它不知道要告訴其父所需的大小,並且在創建它時也沒有layoutParams。

簡而言之,自定義視圖比繪畫更費力。

在任何情況下,而不是一個scrollView包裝它的linearLayout,爲什麼不在裏面使用listView和imageViews?它的速度更快,更簡單,並且內存效率更高(想象一下,爲了擁有100個imageViews,你會做些什麼...)。

更多的信息,看看這個視頻:

http://www.youtube.com/watch?v=wDBM6wVEO70

相關問題