導航:首頁 > 操作系統 > android代碼中設置圖片

android代碼中設置圖片

發布時間:2024-06-26 06:39:49

android編程中怎麼在屏幕上顯示圖片

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shu" />
說明幾點:
1、項目中資源文件名稱不要用中文,把「樹」改成"shu"
2、啟動一個模擬器或者先連接自己的手機,在DDMS查看,如果有設備說明連接成功了,這時就可以運行這個程序了。

Ⅱ android設置背景圖片

教你如何設置背景圖片
xml文件設置背景圖片中:
任意一個控制項,button imageView 或layout,在其的xml屬性設置中,添加

[java] view plain
android:background="@drawable/bg"

即可實現設置其背景圖片為bg.
其中bg.bnp圖片存放在drawable目錄下。
drawable目錄下存放大小圖標共用的圖片。drawable-hdpi中存放240 WVGA800 的模擬器或板子用的圖片。drawable-mdpi存放對應的小圖片

[java] view plain
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pic"/>

同樣,用src屬性,也可設置imageView的圖片為pic.png

java代碼中設置或更改一個控制項的背景圖片:

[java] view plain
Resources resources = getContext().getResources();
Drawable btnDrawable = resources.getDrawable(R.drawable.layout_bg);
layout.setBackgroundDrawable(btnDrawable);

程序中,動態修改layout的背景圖片,用setBackgroundDrawable()函數實現。設置其背景圖片為 layout_bg.png

修改imageView1的背景圖片為imageView2的背景圖片:
imageView2.getDrawable()可獲得圖片2的背景。值為Drawable類型
imageView1.setImageDrawable(drawable); 設置imageView1的背景圖片

即:

[java] view plain
imageView1.setImageDrawable(imageView2.getDrawable());

Ⅲ android中怎麼實現動態設置背景圖片的功能,我在網上搜到的只能設置到當前的頁面,並不能實現設置到全部

當關閉重新運行,它又會自動跳回原始的背景圖片。

在開發過程中,由於使用模擬器測試了程序,在首次運行後會將res文件夾下的圖片資源文件(如drawable-hdpi、drawable-ldpi和drawable-mdpi)拷貝到bin文件夾下。在替換資源圖片後,eclipse並不清楚是否有圖片改變,所以會使用原來bin下的res文件夾中的資源文件進行打包,而圖片用的還是第一次eclipse所拷貝進去的文件,所以當運行程序後會發現替換資源圖片在程序中沒起作用。

解決辦法:每次運行前,清理項目


動態設置背景圖片代碼

privateinti=0;//全局變數定義,初始化

//list數組接收到從文件中讀取到的數據

List<String>list=readTxt.getDierguanResource();

//changeBack這個函數用來動態設置背景圖片

publicvoidchangeBack(intbackground){

main=(LinearLayout)findViewById(R.id.shizi);

Stringa=list.get(background);

//獲取到的背景圖片名as(圖片存到res/drawable文件下)

Stringas=a.split("")[1];

//動態獲取圖片getResources().getIdentifier(as,"drawable",getPackageName())

intresID=getResources().getIdentifier(as,"drawable",getPackageName());

//設置頁面背景setBackgroundResource()

main.setBackgroundResource(resID);

}

if(i>=0&&i<list.size()){


changeBack(i);

}

Ⅳ android 怎樣給activity添加背景圖

設置Activity圖片背景
Android(Activity)設冊辯置背景圖片方法:
xml布局中用andriod:background = "@drawable/bgimage"或者在州汪缺代碼中使用layout.setBackgroundResource(resId)
其陵春中「bgimage」是放在res/drawable/目錄下的需要設置成背景的圖片。
方法一:在main.xml 文件中添加屬性:android:background="@drawable/bgimage",其中
bgimage是drawable目錄下的圖片文件名,圖片會自動縮放至全屏。
如:
<LinearLayout xmlns:android=""
xmlns:tools=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tsv1_0000_0001.skin.Select"
android:background="@drawable/base" >

Ⅳ android中button上設置圖片

android中button上設置圖片的方法為:
1、自定義MyButton類
public class MyButton extends Button {
//This constructormust be
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyButton(Context context) {
super(context);
}
private Paint mPaint = null;
private String mText;
private int mX, mY;
public void onSetText(String text, int nLeft, int nBottom, int nTextSize,
int nTextColor) {
mPaint = new Paint();
mPaint.setTextSize(nTextSize);
mPaint.setColor(nTextColor);
this.mText = text;
this.mX = nLeft;
this.mY = nBottom;
}
private int mDownBmpId, mUpBmpId;
public void onSetBmp(int nDownID, int nUpID) {
this.mDownBmpId = nDownID;
this.mUpBmpId = nUpID;
}
@Override
public void onDraw(Canvas canvas) {
if (mPaint != null)
canvas.drawText(mText, mX, mY, mPaint);
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
super.setBackgroundResource(mDownBmpId);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
super.setBackgroundResource(mUpBmpId);
}
return super.onTouchEvent(event);
}
}

2、 在xml布局文件中添加MyButton控制項,像應用普通的Button控制項一樣。
<com.MyButton
android:id="@+id/test_btn" android:layout_width="120px"
android:layout_height="fill_parent" android:text="Test"
android:background="@drawable/btn_u" />
其中com.MyButton是你定義的MyButton類所在的包名
3、在onCreate()中載入MyButton控制項。
MyButton btn = (MyButton)findViewById(R.id.test_btn);
btn.onSetBmp(R.drawable.btn_d, R.drawable.btn_u);
其中btn_d表示為按下btn時背景圖片,btn_u為默認狀態下btn背景圖片。

閱讀全文

與android代碼中設置圖片相關的資料

熱點內容
原神的伺服器如何分辨 瀏覽:886
每天pdf 瀏覽:518
安卓手機怎麼設置關閉屏幕時間 瀏覽:815
雲伺服器有寶塔面板嗎 瀏覽:518
中行app如何看基點 瀏覽:204
怎麼限制網路連接的人數安卓 瀏覽:177
怎麼把app數據清理 瀏覽:834
什麼APP既 瀏覽:227
兩台阿里雲伺服器怎麼並行 瀏覽:633
觸控筆如何關閉app 瀏覽:123
linux永久關閉防火牆命令 瀏覽:401
spsspdf下載 瀏覽:689
奧維坐標怎麼加密 瀏覽:720
常用的負載均衡演算法 瀏覽:405
java萬年歷源碼 瀏覽:881
如何將安卓手機搬家到蘋果手機 瀏覽:440
怎麼改wifi加密方式 瀏覽:415
pic單片機串口 瀏覽:256
擾流演算法 瀏覽:426
什麼伺服器可以覆蓋城市 瀏覽:562