在一些APP的界面中規劃出一部分空間用於圖片廣告的展示,是不錯的方式。可以美化界面,又能做些廣告。
實例從《android應用案例開發大全》的第一章中的實例中抽出來。這樣以後哪兒要用就可以將這個放在哪兒。(發揚了一下拿來主義,希望不要追究我版權,哈哈)
首先是在佈局文件中設置了這樣的控件,佈局文件activity_gg.xml如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:orientation="vertical"
android:layout_width="200dip"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:layout_marginTop="10dip"
android:layout_weight="2"
android:layout_marginBottom="10dip">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="just take the place"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_marginLeft="20dip"
android:layout_marginRight="15dip"
android:layout_marginBottom="10dip"
android:layout_marginTop="10dip"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<com.nuist.ggview.GGView
android:layout_width="200dp"
android:layout_height="250dp"
/>
</LinearLayout>
</LinearLayout>
其中GGView就是這個控件。其實實現的GGView.java的實現如下:
package com.nuist.ggview;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class GGView extends View {
int COMPONENT_WIDTH; //該控件寬度
int COMPONENT_HEIGHT; //該控件高度
boolean initflag=false; //是否要獲取控件的高度和寬度標誌
static Bitmap[] bma; //需要播放的圖片的數組
Paint paint; //畫筆
int[] drawablesId; //圖片ID數組
int currIndex=0; //圖片ID數組下標,根據此變量畫圖片
boolean workFlag=true; //播放圖片線程標誌位
public GGView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
drawablesId=new int[]{ //初始化圖片ID數組
R.drawable.adv1, //將需要播放的圖片ID放於此處即可
R.drawable.adv2,
R.drawable.adv3,
};
bma=new Bitmap[drawablesId.length]; //創建存放圖片的數組
initBitmaps(); //調用初始化圖片函數,初始化圖片數組
paint=new Paint(); //創建畫筆
paint.setFlags(Paint.ANTI_ALIAS_FLAG); //消除鋸齒
new Thread(){ //創建播放圖片線程
public void run(){
while(workFlag){
currIndex=(currIndex+1)%drawablesId.length;//改變ID數組下標值
GGView.this.postInvalidate(); //繪製
try {
Thread.sleep(3000); //休息三秒
} catch (InterruptedException e) {
e.printStackTrace();
}}}}.start();
}
public void initBitmaps(){ //初始化圖片函數
Resources res=this.getResources(); //獲取Resources對象
for(int i=0;i<drawablesId.length;i++){
bma[i]=BitmapFactory.decodeResource(res, drawablesId[i]);
}}
public void onDraw(Canvas canvas){ //繪製函數
if(!initflag) { //第一次繪製時需要獲取寬度和高度
COMPONENT_WIDTH=this.getWidth(); //獲取view的寬度
COMPONENT_HEIGHT=this.getHeight(); //獲取view的高度
initflag=true;
}
int picWidth=bma[currIndex].getWidth(); //獲取當前繪製圖片的寬度
int picHeight=bma[currIndex].getHeight(); //獲取當前繪製圖片的高度
int startX=(COMPONENT_WIDTH-picWidth)/2; //得到繪製圖片的左上角X座標
int startY=(COMPONENT_HEIGHT-picHeight)/2; //得到繪製圖片的左上角Y座標
canvas.drawARGB(255, 200, 128, 128); //設置背景色
canvas.drawBitmap(bma[currIndex], startX,startY, paint); //繪製圖片
}
}
所需使用的三張圖片,可以自己添加到res/drawable下
MainActivity如下:
package com.nuist.ggview;
import android.os.Bundle;
import android.app.Activity;
public class GGActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gg);
}
}
程序運行結果,圖片是動態顯示的,三秒會切換一張圖片。主要是這個模塊化了的顯示圖片的控件覺得蠻好用。