如何在Android中绘制适合整个手机屏幕的图像

时间:2015-04-30 06:19:32

标签: android android-layout svg imageview

我需要在活动屏幕的内容区域中显示背景图片(bitmapSVG资源)。我将构建一些UI元素,如按钮,图像位于此背景图像之上。

所以我的查询是,

  1. 为了让应用在所有主要手机屏幕上保持一致,我需要使用的背景图像的理想宽度和高度应该是什么
  2. 我使用RelativeLayout,以便我可以从代码中以编程方式添加更多UI元素。在这种情况下,我应该将ImageView的属性设置为完全适合屏幕。
  3. 是否有像Android iconography这样的标准指南用于设计带有拉伸手机屏幕宽度和高度的背景图像的布局
  4. 我尝试过没有成功的事情包括

    1。为ImageView设置adjustViewBounds = true

    <ImageView
            android:id="@+id/manga_page_image"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter" />
    
    1. 以编程方式设置

      public class ScalingImages extends Activity {
      
      private ImageView imageView;
      
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.test_margin);
      
          this.imageView = (ImageView)findViewById(R.id.news_image);
      
          // The image is coming from resource folder but it could also 
          // load from the internet or whatever
          Drawable drawable = getResources().getDrawable(R.drawable.img);
          Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
      
          // Get scaling factor to fit the max possible width of the ImageView
          float scalingFactor = this.getBitmapScalingFactor(bitmap);
      
          // Create a new bitmap with the scaling factor
          Bitmap newBitmap = Util.ScaleBitmap(bitmap, scalingFactor);
      
          // Set the bitmap as the ImageView source
          this.imageView.setImageBitmap(newBitmap);
      }
      
      private float getBitmapScalingFactor(Bitmap bm) {
          // Get display width from device
          int displayWidth = getWindowManager().getDefaultDisplay().getWidth();
      
          // Get margin to use it for calculating to max width of the ImageView
          LinearLayout.LayoutParams layoutParams = 
              (LinearLayout.LayoutParams)this.imageView.getLayoutParams();
          int leftMargin = layoutParams.leftMargin;
          int rightMargin = layoutParams.rightMargin;
      
          // Calculate the max width of the imageView
          int imageViewWidth = displayWidth - (leftMargin + rightMargin);
      
          // Calculate scaling factor and return it
          return ( (float) imageViewWidth / (float) bm.getWidth() );
      }
      }
      
    2. 其中

      public class Util {
      
          public static Bitmap ScaleBitmap(Bitmap bm, float scalingFactor) {
              int scaleHeight = (int) (bm.getHeight() * scalingFactor);
              int scaleWidth = (int) (bm.getWidth() * scalingFactor);
      
              return Bitmap.createScaledBitmap(bm, scaleWidth, scaleHeight, true);
          }
      
      }
      

      我仍然没有尝试使用SurfaceView选项,因为我认为它是资源cruncher,因为它每次尝试加载图像。我想知道有没有更好的方法来做到这一点。

0 个答案:

没有答案