android background

你知道吗?windowBackground会影响到Activity的绘制速度! 
        而我们在开发android应用时,很多时候并没有注意到这些。因为我们大多数时候,不会更改Activity主题风格,于是android使用默认的主题风格。而这个主题风格中就包含有一个 windowBackground。这个默认的主题风格定义在frameworks\base\core\res\res\values\themes.xml 中,如下: 

  1. <style name="Theme">  
    <item name="colorForeground">@android:color/bright_foreground_dark</item>  
    <item name="colorForegroundInverse">@android:color/bright_foreground_dark_inverse</item>  
    <item name="colorBackground">@android:color/background_dark</item>  
    <item name="colorBackgroundCacheHint">?android:attr/colorBackground</item>  
    <item name="disabledAlpha">0.5</item>      
    <item name="backgroundDimAmount">0.6</item>
    ......
    <item name="windowBackground">@android:drawable/screen_background_dark</item>        
    <item name="windowFrame">@null</item>        
    <item name="windowNoTitle">false</item>      
    <item name="windowFullscreen">false</item>        
    <item name="windowIsFloating">false</item>      
    <item name="windowContentOverlay">@android:drawable/title_bar_shadow</item>        
    <item name="windowShowWallpaper">false</item>  


 
这个默认的 windowBackground 定义为screen_background_dark,可以在frameworks\base\core\res\res\values\colors.xml 中找到,定义为:<drawable name="screen_background_dark">#ff000000</drawable> 
 
我们首先来看看windowBackground 对程序的影响,然后再找出影响的原因。 
 
1.新建一个叫做windowBackground的工程,其它步骤略。注意由于没有修改主题,所以工程中的activity使用默认的主题风格。下面在res/layout/main.xml 中添加一个FpsImageView,如下: 

<?xml version="1.0" encoding="utf-8"?>
<!--  This layout does not use <merge/> to slow down the drawing --><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent">
    <com.example.android.window.FpsImageView        android:layout_width="fill_parent"        android:layout_height="fill_parent"                android:src="@drawable/antelope_canyon"        android:scaleType="center" />
    <TextView        android:id="@+id/title"            android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal|bottom"        android:layout_marginBottom="12dip"                android:padding="12dip"                android:background="#90000000"        android:textAppearance="?android:attr/textAppearanceLarge"        android:text="Antelope Canyon" />        
</FrameLayout>


 
其中的FpsImageView继承自ImageView,在其中增加了 帧率的计算。在activity中不停的调用postInvalidate以使得FpsImageView不停的绘制,从而显示出帧率。FpsImageView代码如下: 

  1. public class FpsImageView extends ImageView {
  2.     private long mStartTime = -1;
  3.     private int mCounter;
  4.     private int mFps;
  5.     private final Paint mPaint;
  6.     
  7.     public FpsImageView(Context context, AttributeSet attrs) {
  8.         super(context, attrs);
  9.         
  10.         mPaint = new Paint();
  11.         mPaint.setColor(0xFFFFFFFF);
  12.         mPaint.setAntiAlias(true);
  13.     }
  14.     @Override
  15.     public void draw(Canvas canvas) {
  16.         if (mStartTime == -1) {
  17.             mStartTime = SystemClock.elapsedRealtime();
  18.             mCounter = 0;
  19.         }
  20.         long now = SystemClock.elapsedRealtime();
  21.         long delay = now - mStartTime;
  22.         super.draw(canvas);
  23.         
  24.         canvas.drawText(mFps + " fps", 100, 50, mPaint);
  25.         if (delay > 1000L) {
  26.             mStartTime = now;
  27.             mFps = mCounter;
  28.             mCounter = 0;
  29.         }
  30.         
  31.         mCounter++;
  32.     }
  33. }

 
 
运行程序,得到如下结果: 
 
可以看到目前的帧率大概在44 左右。 
 
 
2.修改AndroidManifest.xml,为activity指定主题,即不使用默认主题,在activity标签内添加一行: 
android:theme="@style/Theme.NoBackground" 
其中的 Theme.NoBackground 定义在res/values/theme.xml中,如下: 

  1. <?xml version="1.0" encoding="utf-8"?><resources>    <style name="Theme.NoBackground" parent="android:Theme">        <item name="android:windowBackground">@null</item>    </style></resources>

 
 
 
从theme.xml中可以看出,它将android:windowBackground置为null了,使用此主题,我们的activity讲没有背景  运行程序,得到如下结果: 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Android中设置背景图片,可以通过以下步骤实现: 1. 在res目录下创建一个drawable文件夹,将要设置的图片放入该文件夹中。 2. 在布局文件中,使用android:background属性来设置背景图片,例如: <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background_image"> </LinearLayout> 其中,@drawable/background_image表示要设置的图片名称。 3. 如果需要在Java代码中动态设置背景图片,可以使用setBackgroundResource()方法,例如: LinearLayout layout = findViewById(R.id.layout); layout.setBackgroundResource(R.drawable.background_image); 其中,R.drawable.background_image表示要设置的图片资源ID。 ### 回答2: 在Android中设置背景图片可以通过使用ImageView或者是在布局文件的背景属性中设置。以下是两种常用的方法: 1. 使用ImageView: 首先,在你的布局文件中添加一个ImageView,并给它设置一个唯一的id,如下所示: ``` <ImageView android:id="@+id/imageView_background" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop"/> ``` 然后在你的Activity或Fragment中找到这个ImageView并设置它的背景图片,如下所示: ``` ImageView imageViewBackground = findViewById(R.id.imageView_background); imageViewBackground.setImageResource(R.drawable.background_image); ``` 其中,R.drawable.background_image是你需要设置的背景图片资源。你也可以使用其他的ImageView属性来调整图片的显示效果,如scaleType来设置图片的缩放类型。 2. 在布局文件中设置背景属性:另一种方法是直接在布局文件的根布局中设置背景属性,如下所示: ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background_image"> <!-- 布局的其他内容 --> </RelativeLayout> ``` 这里的@drawable/background_image是你需要设置的背景图片资源。你可以根据具体需要选择不同的布局文件和背景属性来使用。 无论你选择哪种方法,都要确保背景图片的尺寸和分辨率适配当前设备,避免出现拉伸或变形。 ### 回答3: 在Android中,我们可以通过设置background属性来将图片设置为View或布局的背景。可以将图片资源引用作为background属性的值,也可以通过代码动态设置背景图片。 首先,如果要通过资源引用设置背景图片,需要将图片文件放置在res/drawable或res/drawable-*dpi目录下。然后,在XML布局文件中找到要设置背景图片的View,给该View的background属性赋值为"@drawable/图片文件名",即可将该图片作为背景。 例如,要将一张名为"bg_image.jpg"的图片设置为TextView的背景,可以在XML布局文件中找到该TextView的对应代码块,设置其background属性为"@drawable/bg_image"。 另外,我们也可以通过代码动态地设置背景图片。首先,找到对应的View对象,使用setBackgroundResource()方法来设置背景图片,参数传入要设置的图片资源id。例如,要将一张名为"bg_image.jpg"的图片设置为ImageView的背景,可以在Java代码中找到该ImageView的对象,然后调用setBackgroundResource(R.drawable.bg_image),即可将该图片作为背景。 需要注意的是,为了适应不同屏幕密度和尺寸的设备,建议提供不同分辨率的图片资源放置在不同drawable目录中。此外,还可以使用VectorDrawable或者NinePatch图像来适应不同分辨率和屏幕尺寸的要求。 总结起来,通过设置background属性,可以在Android中很方便地将图片作为View或布局的背景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值