androidimageview自定義
① android中如何動態修改ImageView中圖片的大小 比如說點擊該圖片後圖片能變大
使用點陣圖(Bitmap)再重新繪制遍
//原點陣圖
Bitmap btm1 = BitmapFactory.decodeResource(Main.this.getResources(), R.drawable.hh);
BitmapDrawable bd1=BitmapDrawable(btm1);
imageView.setBackgroundDrawable(bd1);
放大後 滑動時計算 圖片的邊是否在屏幕外,如果在屏幕外就可移動,比如 向上滑動,是想看圖片下面的部分,此時計算圖片的底邊是否在屏幕外,如果是,就可以向上移動圖片。
Scaletype的設置的方式包括:
1、在layout xml中定義android:scaleType="CENTER"
2、或在代碼中調用imageView.setScaleType(ImageView.ScaleType.CENTER);
(1)androidimageview自定義擴展閱讀:
根據位深度,可將點陣圖分為1、4、8、16、24及32點陣圖像等。每個像素使用的信息位數越多,可用的顏色就越多,顏色表現就越逼真,相應的數據量越大。
例如,位深度為 1 的像素點陣圖只有兩個可能的值(黑色和白色),所以又稱為二值點陣圖。位深度為 8 的圖像有 2^8(即 256)個可能的值。位深度為 8 的灰度模式圖像有 256 個可能的灰色值。
RGB圖像由三個顏色通道組成。8 位/通道的 RGB 圖像中的每個通道有 256 個可能的值,這意味著該圖像有 1600 萬個以上可能的顏色值。
有時將帶有 8 位/通道 (bpc) 的 RGB 圖像稱作 24 點陣圖像(8 位 x 3 通道 = 24 位數據/像素)。通常將使用24位RGB組合數據位表示的的點陣圖稱為真彩色點陣圖。
② android 自定義圖片選擇器,怎麼篩選圖片
偽代碼如下:
private boolean flag;
public void onClick(View v){
if(flag){
mImageView.setImageResource(R.drawable.xx1);
}else{
mImageView.setImageResource(R.drawable.xx2);
}
flag = !flag;
}123456789123456789
筆者連上面的代碼知道寫出來那為什麼還要去自定義一個ImageView了?
具體需求:兩個ImageView之間實現單選效果
我們試想下,目前兩個ImageView通過上面的代碼可能還好,只要在不同的事件中做出不同的判斷就好了,但如果一但ImageView增多了了?
A:你不知道用 RadioGroup+RadioButton 啊!
B:是哦!我現在去試下。
……
B:不行啊,雖然RadioButton可以實現,但不好做適配,我為RadioButton設置Drawable,不能居中,而且不能隨著RadioButton的大小改變而改變,資源圖片是多大就多大,顯示區域不夠就不能完全顯示出來。
A:…?,額,是嗎?這樣啊!那我們就自定義一個ImageView來實現吧!
B:為什麼是自定義ImageView?而不是自定義RadioButton?
A:自定義RadioButton實現ImageView的src屬性比較復雜(等著正在看這博客的大神實現),而自定義ImageView來實現單選的屬性比較好實現。
B:那怎麼實現了?
A:看代碼,代碼如下:
attrs.xml <為自定義ImageView添加兩個屬性>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SelectorImageView">
<attr name="selector_src" format="reference"/>//選中的src圖片屬性
<attr name="checked" format="boolean"/>
</declare-styleable>
</resources>12345671234567
Class - SelectorImageView<此類實現了Checkable介面,這里沒什麼特殊功能,而只是利用此介面中的方法而已,不實現我們也可以自己寫>
public class SelectorImageView extends ImageView implements Checkable {
private boolean isChecked;
private Drawable mSelectorDrawable;
private Drawable mDrawable;
public SelectorImageView(Context context) {
this(context, null);
}
public SelectorImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SelectorImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
/**獲取默認屬性src的Drawable並用成員變數保存*/
mDrawable = getDrawable();
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SelectorImageView);
/**獲取自定義屬性selector_src的Drawable並用成員變數保存*/
Drawable d = a.getDrawable(R.styleable.SelectorImageView_selector_src);
mSelectorDrawable = d;
/**獲取自定義屬性checked的值並用成員變數保存*/
isChecked = a.getBoolean(R.styleable.SelectorImageView_checked, false);
setChecked(isChecked);
if (d != null && isChecked) {
/**如果在布局中設置了selector_src與checked = true,我們就要設置ImageView的圖片為mSelectorDrawable */
setImageDrawable(d);
}
a.recycle();
}
@Override
public void setImageDrawable(Drawable drawable) {
super.setImageDrawable(drawable);
}
@Override
public void setChecked(boolean checked) {
this.isChecked = checked;
}
@Override
public boolean isChecked() {
return isChecked;
}
@Override
public void toggle() {
/**此處依據是否選中來設置不同的圖片*/
if (isChecked()) {
setImageDrawable(mSelectorDrawable);
} else {
setImageDrawable(mDrawable);
}
}
public void toggle(boolean checked){
/**外部通過調用此方法傳入checked參數,然後把值傳入給setChecked()方法改變當前的選中狀態*/
setChecked(checked);
toggle();
}
}
layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.qjay.adf.widget.SelectorImageView
android:id="@+id/iv"
android:layout_width="100dp"
android:layout_height="100dp"
app:selector_src="@mipmap/checked"
android:src="@mipmap/no_checked"/>
</LinearLayout>12345678910111234567891011
Activity Code
public class MainActivity extends Activity {
private SelectorImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (SelectorImageView) findViewById(R.id.iv);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
iv.toggle(!iv.isChecked());
}
});
}
}
③ android怎麼自定imagebiew的組合控制項
1.先定義該組合的XML文件布局
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="wrap_content"
4 android:layout_height="wrap_content"
5 android:orientation="horizontal" >
6 <LinearLayout
7 android:layout_width="wrap_content"
8 android:layout_height="wrap_content"
9 android:orientation="vertical"
10 >
11 <ImageView
12 android:id="@+id/touxiang"
13 android:layout_width="80dp"
14 android:layout_height="80dp"
15 android:maxWidth="80dp"
16 android:maxHeight="80dp"
17 >
18 </ImageView>
19 <ImageView
20 android:id="@+id/blood"
21 android:layout_width="wrap_content"
22 android:layout_height="wrap_content"
23 android:maxWidth="80dp"
24 android:maxHeight="20dp"
25 >
26 </ImageView>
27
28 </LinearLayout>
29
30 <LinearLayout
31 android:layout_width="wrap_content"
32 android:layout_height="wrap_content"
33 android:orientation="vertical"
34 android:layout_gravity="center_vertical"
35 >
36 <TextView
37 android:layout_width="wrap_content"
38 android:layout_height="wrap_content"
39 android:text="武器"
40 ></TextView>
41 <TextView
42 android:layout_width="wrap_content"
43 android:layout_height="wrap_content"
44 android:text="防具"
45 ></TextView>
46 <TextView
47 android:layout_width="wrap_content"
48 android:layout_height="wrap_content"
49 android:text="+1馬"
50 ></TextView>
51 <TextView
52 android:layout_width="wrap_content"
53 android:layout_height="wrap_content"
54 android:text="-1馬"
55 ></TextView>
56 </LinearLayout>
57
58 </LinearLayout>
2.自定義一個繼承布局的類
public class GeneralFrame extends LinearLayout {
ImageView general;
ImageView blood;
TextView wuqi;
TextView fangju;
TextView jiayima;
TextView jianyima;
public GeneralFrame(Context context) {
//super(context);
// TODO Auto-generated constructor stub
this(context,null);
}
public GeneralFrame(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
//在構造函數中將Xml中定義的布局解析出來。
LayoutInflater.from(context).inflate(R.layout.generalframe, this, true);
general=(ImageView)findViewById(R.id.touxiang);
blood=(ImageView)findViewById(R.id.blood);
blood.setImageResource(R.drawable.blood);
//wuqi=(TextView)findViewById(R.id
}
可在XML文件里調用該類
< com.layouts.uitest.GeneralFrame
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
也可以在代碼中動態添加該類
GeneralFrame general=new GeneralFrame(this);
general.setGeneralImage(R.drawable.diaochan);
linear2.addView(general);
自定義View.
自定義類繼承View
public class MyView extends View
{
public MyView (Context c,AttributeSet set)
{
}
@Override
public void onDraw(Canvas canvas)
{
}
}
調用方法同自定義控制項一樣。
自定義View的構造方法一定要選中 public MyView (Context c,AttributeSet set),系統會回調該構造方法
④ android實現自定義imageView
可以的,自定義的view裡面按照你的需求來進行布局,是可以的,比如你背景是一個大的圖片,然後在上面再疊加一個imageview這些都是可以的。
你說的示例關於你這個imageview的沒有,不過你自己定義linearlayout的話,理論上來說可以實現任意布局。下面是我最近的一個項目裡面用到的布局,是自己定義的titlebar。你參考一下吧。望採納
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/topBar"
android:layout_width="match_parent"
android:layout_height="@dimen/actionbar_height"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:id="@+id/left_title_bar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@string/conversation_list_name"
android:textColor="@drawable/asp_selector_tab_textcolor"
android:textSize="@dimen/contact_tab_font_size" />
<TextView
android:id="@+id/right_title_bar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@string/favorite_list_name"
android:textColor="@drawable/asp_selector_tab_textcolor"
android:textSize="@dimen/contact_tab_font_size" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" >
<ImageView
android:id="@+id/titlebar_indicate_left"
android:layout_width="fill_parent"
android:layout_height="3dip"
android:layout_weight="1.0"
android:scaleType="matrix"
android:src="@color/contact_detail_normal_blue" />
<ImageView
android:id="@+id/titlebar_indicate_right"
android:layout_width="fill_parent"
android:layout_height="3dip"
android:layout_weight="1.0"
android:scaleType="matrix"
android:src="@color/contact_detail_normal_blue"
android:visibility="invisible" />
</LinearLayout>
</LinearLayout>
⑤ android 自定義imagView添加文本,求思路和demo
imageView是在那個relativeLayout裡面添加的還是說直接將圖片設置為相對布局的背景?還有動態添加文字這部分是在代碼中實現的吧,繼承relativeLayout,重寫方法自定義view?是么
⑥ android 自定義imageview 線程怎麼停止
1 良好的自定義View
易用,標准,開放。
一個設計良好的自定義view和其他設計良好的類很像。封裝了某個具有易用性介面的功能組合,這些功能能夠有效地使用CPU和內存,並且十分開放的。但是,除了開始一個設計良好的類之外,一個自定義view應該:
l 符合安卓標准
l 提供能夠在Android XML布局中工作的自定義樣式屬性
l 發送可訪問的事件
l 與多個Android平台兼容。
Android框架提供了一套基本的類和XML標簽來幫您創建一個新的,滿足這些要求的view。忘記提供屬性和事件是很容易的,尤其是當您是這個自定義view的唯一用戶時。請花一些時間來仔細的定義您view的介面以減少未來維護時所耗費的時間。一個應該遵從的准則是:暴露您view中所有影響可見外觀的屬性或者行為。
2 創建自定義View (步驟)
2.1 繼承View完全自定義或繼承View的派生子類
必須提供一個能夠獲取Context和作為屬性的AttributeSet對象的構造函數,獲取屬性,當view從XML布局中創建了之後,XML標簽中所有的屬性都從資源包中讀取出來並作為一個AttributeSet傳遞給view的構造函數。
View 派生出來的直接或間接子類:ImageView, Button, CheckBox, SurfaceView, TextView, ViewGroup, AbsListView
ViewGourp 派生出來的直接或間接子類:AbsoluteLayout, FrameLayout, RelativeLayout, LinearLayout
所有基類、派生類都是Android framework層集成的標准系統類, 可直接引用SDK中這些系統類及其API
2.2 定義自定義屬性
l 在資源元素<declare-styleable>中為您的view定義自定義屬性。
在項目組添加<declare-styleable>資源。這些資源通常是放在res/values/attrs.xm文件里。如下是attrs.xml文件的一個例子:
<resources>;
<declare-styleable name="PieChart">
<attr name="showText" format="boolean" />
<attr name="labelPosition" format="enum">
<enum name="left" value="0"/>
<enum name="right" value="1"/>
</attr>
</declare-styleable>
</resources>
l 在您的XML布局中使用指定屬性的值。
布局XML文件中可以像內建屬性一樣使用它們。唯一不同是自定義屬性屬於不同的命名空間。
http://schemas.android.com/apk/res/[你的自定義View所在的包路徑]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews">
<com.example.customviews.charting.PieChart
custom:showText="true"
custom:labelPosition="left" />
</LinearLayout>
l 在運行時取得屬性值。
l 將取回的屬性值應用到您的view中。
2.3 獲取自定義屬性
當view從XML布局中創建了之後,XML標簽中所有的屬性都從資源包中讀取出來並作為一個AttributeSet傳遞給view的構造函數。盡管從AttributeSet中直接讀取值是可以的,但是這樣做有一些缺點:
l 帶有值的資源引用沒有進行處理
l 樣式並沒有得到允許
取而代之的是,將AttributeSet傳遞給obtainStyledAttributes()方法。這個方法傳回了一個TypedArray數組,包含了已經解除引用和樣式化的值。
為了時能能夠更容易的調用obtainStyledAttributes()方法,Android資源編譯器做了大量的工作。res文件夾 中的每個<declare-styleable>資源,生成的R.java都定義了一個屬性ID的數組以及一套定義了指向數組中的每一個屬性 的常量。您可以使用預定義的常量從TypedArry中讀取屬性。下例是PieChart類是如何讀取這些屬性的:
public PieChart(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.PieChart,
0, 0);
try {
mShowText = a.getBoolean(R.styleable.PieChart_showText, false);
mTextPos = a.getInteger(R.styleable.PieChart_labelPosition, 0);
} finally {
a.recycle();
}
}
注意,TypedArry對象是一個共享的資源,使用完畢必須回收它。
2.4 添加屬性和事件
屬性是控制view的行為和外觀的強有力的方式,但是只有view在初始化後這些屬性才可讀。為了提供動態的行為,需要暴露每個自定義屬性的一對getter和setter。下面的代碼片段顯示PieChart是如何提供showText屬性的。
public boolean isShowText() {
return mShowText;
}
public void setShowText(boolean showText) {
mShowText = showText;
invalidate();
requestLayout();
}
注意,setShowText調用了invalidate()和requestLayout()。這些調用關鍵是為了保證view行為是可靠的。你必須在改變這個可能改變外觀的屬性後廢除這個view,這樣系統才知道需要重繪。同樣,如果屬性的變化可能影響尺寸或者view的形狀,您需要請求一個新的布局。忘記調用這些方法可能導致難以尋找的bug。
自定義view同樣需要支持和重要事件交流的事件監聽器。
2.5 自定義繪制(實施)
繪制自定義視圖里最重要的一步是重寫onDraw()方法. onDraw()的參數是視圖可以用來繪制自己的Canvas對象. Canvas定義用來繪制文本、線條、點陣圖和其他圖像單元. 你可以在onDraw()里使用這些方法創建你的自定義用戶界面(UI).
android.graphics框架把繪圖分成了兩部分:
l 畫什麼, 由Canvas處理
l 怎麼畫, 由Paint處理
例如, Canvas提供畫線條的方法, 而Paint提供定義線條顏色的方法. Canvas提供畫矩形的方法, 而Paint定義是否用顏色填充矩形或讓它為空. 簡而言之, Canvas定義你可以在屏幕上畫的形狀, 而Paint為你畫的每個形狀定義顏色、樣式、字體等等.
onDraw()不提供3d圖形api的支持。如果你需要3d圖形支持,必須繼承SurfaceView而不是View,並且通過單獨的線程畫圖。
3 優化
3.1 降低刷新頻率
為了提高view的運行速度,減少來自於頻繁調用的程序的不必要的代碼。從onDraw()方法開始調用,這會給你帶來最好的回報。特別地,在onDraw()方法中你應該減少冗餘代碼,冗餘代碼會帶來使你view不連貫的垃圾回收。初始化的冗餘對象,或者動畫之間的,在動畫運行時,永遠都不會有所貢獻。
加之為了使onDraw()方法更有依賴性,你應該盡可能的不要頻繁的調用它。大部分時候調用 onDraw()方法就是調用invalidate()的結果,所以減少不必要的調用invalidate()方法。有可能的,調用四種參數不同類型的invalidate(),而不是調用無參的版本。無參變數需要刷新整個view,而四種參數類型的變數只需刷新指定部分的view.這種高效的調用更加接近需求,也能減少落在矩形屏幕外的不必 要刷新的頁面。
3.2 使用硬體加速
作為Android3.0,Android2D圖表系統可以通過大部分新的Android裝置自帶GPU(圖表處理單元)來增加,對於許多應用程序 來說,GPU硬體加速度能帶來巨大的性能增加,但是對於每一個應用來講,並不都是正確的選擇。Android框架層更好地為你提供了控制應用程序部分硬體 是否增加的能力。
怎樣在你的應用,活動,或者窗體級別中使用加速度類,請查閱Android開發者指南中的Hardware Acceleration類。注意到在開發者指南中的附加說明,你必須在你的AndroidManifest.xml 文件中的<uses-sdk android:targetSdkVersion="11"/>中將應用目標API設置到11或者更高的級別。
一旦你使用硬體加速度類,你可能沒有看到性能的增長,手機GPUs非常擅長某些任務,例如測量,翻轉,和平移點陣圖類的圖片。特別地,他們不擅長其他的任務,例如畫直線和曲線。為了利用GPU加速度類,你應該增加GPU擅長的操作數量,和減少GPU不擅長的操作數量。
⑦ android 如何重寫imageview 讓圖片有圓角效果
android 自定義圓角ImageView以及鋸齒的處理
看到很多人開發過程中要使用圓角圖片時,解決方法有:
1.重新繪制一張圖片
2.通過布局來配置
3.通過重寫View來實現
其中1,2在這里就不講了,重點講講方法三的實現。
實現一:通過截取畫布一個圓形區域與圖片的相交部分進行繪制,缺點:鋸齒明顯,設置Paint,Canvas抗鋸齒無效。
package com.open.circleimageview.widget;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Region;
import android.util.AttributeSet;
import android.view.View;
public class CircleImageViewA extends View {
public CircleImageViewA(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CircleImageViewA(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CircleImageViewA(Context context) {
super(context);
}
private Bitmap bitmap;
private Rect bitmapRect=new Rect();
private PaintFlagsDrawFilter pdf=new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);
private Paint paint = new Paint();
{
paint.setStyle(Paint.Style.STROKE);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setAntiAlias(true);// 設置畫筆的鋸齒效果。 true是去除,大家一看效果就明白了
}
private Path mPath=new Path();
public void setImageBitmap(Bitmap bitmap)
{
this.bitmap=bitmap;
}
@Override
protected void onDraw(Canvas canvas) {
if(null==bitmap)
{
return;
}
bitmapRect.set(0, 0, getWidth(), getHeight());
canvas.save();
canvas.setDrawFilter(pdf);
mPath.reset();
canvas.clipPath(mPath); // makes the clip empty
mPath.addCircle(getWidth()/2, getWidth()/2, getHeight()/2, Path.Direction.CCW);
canvas.clipPath(mPath, Region.Op.REPLACE);
canvas.drawBitmap(bitmap, null, bitmapRect, paint);
canvas.restore();
}
}
實現二:通過PorterDuffXfermode 方式(注意要設置硬體加速,否則部分機子無效),優點:鋸齒基本沒有
package com.open.circleimageview.widget;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
public class CircleImageViewB extends View {
public CircleImageViewB(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public CircleImageViewB(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CircleImageViewB(Context context) {
super(context);
init();
}
private Bitmap bitmap;
private Rect bitmapRect=new Rect();
private PaintFlagsDrawFilter pdf=new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);
private Paint paint = new Paint();
{
paint.setStyle(Paint.Style.STROKE);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setAntiAlias(true);// 設置畫筆的鋸齒效果。 true是去除,大家一看效果就明白了
}
private Bitmap mDstB=null;
private PorterDuffXfermode xfermode=new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY);
private void init()
{
try {
if(android.os.Build.VERSION.SDK_INT>=11)
{
setLayerType(LAYER_TYPE_SOFTWARE, null);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void setImageBitmap(Bitmap bitmap)
{
this.bitmap=bitmap;
}
private Bitmap makeDst(int w, int h)
{
Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bm);
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setColor(Color.parseColor("#ffffffff"));
c.drawOval(new RectF(0, 0, w, h), p);
return bm;
}
@Override
protected void onDraw(Canvas canvas) {
if(null==bitmap)
{
return;
}
if(null==mDstB)
{
mDstB=makeDst(getWidth(), getHeight());
}
bitmapRect.set(0, 0, getWidth(), getHeight());
canvas.save();
canvas.setDrawFilter(pdf);
canvas.drawBitmap(mDstB, 0, 0, paint);
paint.setXfermode(xfermode);
canvas.drawBitmap(bitmap, null, bitmapRect, paint);
paint.setXfermode(null);
canvas.restore();
}
}
⑧ android ,自定義了一個imageView,在主程序中想為每一個imageView添加事件監聽,但是很麻煩,該怎麼辦
既然是自定義的imageView,你可以直接實現onClickListener啊。
在ImageView創建時,就直接setOnClickListener(this),然後實現OnClickListener不就行了?
⑨ android自定義控制項怎麼用
一、控制項自定義屬性介紹
以下示例中代碼均在values/attrs.xml 中定義,屬性均可隨意命名。
1. reference:參考某一資源ID。
示例:
<declare-styleable name = "名稱">
<attr name = "background" format = "reference" />
<attr name = "src" format = "reference" />
</declare-styleable>
2. color:顏色值。
示例:
<declare-styleable name = "名稱">
<attr name = "textColor" format = "color" />
</declare-styleable>
3. boolean:布爾值。
示例:
<declare-styleable name = "名稱">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
4. dimension:尺寸值。
示例:
<declare-styleable name = "名稱">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>
5. float:浮點值。
示例:
<declare-styleable name = "名稱">
<attr name = "fromAlpha" format = "float" />
<attr name = "toAlpha" format = "float" />
</declare-styleable>
6. integer:整型值。
示例:
<declare-styleable name = "名稱">
<attr name = "frameDuration" format="integer" />
<attr name = "framesCount" format="integer" />
</declare-styleable>
7. string:字元串。
示例:
<declare-styleable name = "名稱">
<attr name = "text" format = "string" />
</declare-styleable>
8. fraction:百分數。
示例:
<declare-styleable name="名稱">
<attr name = "pivotX" format = "fraction" />
<attr name = "pivotY" format = "fraction" />
</declare-styleable>
9. enum:枚舉值。
示例:
<declare-styleable name="名稱">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
10. flag:位或運算。
示例:
<declare-styleable name="名稱">
<attr name="windowSoftInputMode">
<flag name = "stateUnspecified" value = "0" />
<flag name = "stateUnchanged" value = "1" />
<flag name = "stateHidden" value = "2" />
<flag name = "stateAlwaysHidden" value = "3" />
</attr>
</declare-styleable>
11.多類型。
示例:
<declare-styleable name = "名稱">
<attr name = "background" format = "reference|color" />
</declare-styleable>
二、屬性的使用以及自定義控制項的實現
1、構思控制項的組成元素,思考所需自定義的屬性。
比如:我要做一個 <帶陰影的按鈕,按鈕正下方有文字說明>(類似9宮格按鈕)
新建values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="custom_view">
<attr name="custom_id" format="integer" />
<attr name="src" format="reference" />
<attr name="background" format="reference" />
<attr name="text" format="string" />
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>
以上,所定義為custom_view,custom_id為按鈕id,src為按鈕,background為陰影背景,text為按鈕說明,textColor為字體顏色,textSize為字體大小。
2、怎麼自定義控制項呢,怎麼使用這些屬性呢?話不多說請看代碼,CustomView :
package com.nanlus.custom;
import com.nanlus.custom.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomView extends FrameLayout implements OnClickListener {
private CustomListener customListener = null;
private Drawable mSrc = null, mBackground = null;
private String mText = "";
private int mTextColor = 0;
private float mTextSize = 20;
private int mCustomId = 0;
private ImageView mBackgroundView = null;
private ImageButton mButtonView = null;
private TextView mTextView = null;
private LayoutParams mParams = null;
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.custom_view);
mSrc = a.getDrawable(R.styleable.custom_view_src);
mBackground = a.getDrawable(R.styleable.custom_view_background);
mText = a.getString(R.styleable.custom_view_text);
mTextColor = a.getColor(R.styleable.custom_view_textColor,
Color.WHITE);
mTextSize = a.getDimension(R.styleable.custom_view_textSize, 20);
mCustomId = a.getInt(R.styleable.custom_view_custom_id, 0);
mTextView = new TextView(context);
mTextView.setTextSize(mTextSize);
mTextView.setTextColor(mTextColor);
mTextView.setText(mText);
mTextView.setGravity(Gravity.CENTER);
mTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mButtonView = new ImageButton(context);
mButtonView.setImageDrawable(mSrc);
mButtonView.setBackgroundDrawable(null);
mButtonView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mButtonView.setOnClickListener(this);
mBackgroundView = new ImageView(context);
mBackgroundView.setImageDrawable(mBackground);
mBackgroundView.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addView(mBackgroundView);
addView(mButtonView);
addView(mTextView);
this.setOnClickListener(this);
a.recycle();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mParams = (LayoutParams) mButtonView.getLayoutParams();
if (mParams != null) {
mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
mButtonView.setLayoutParams(mParams);
}
mParams = (LayoutParams) mBackgroundView.getLayoutParams();
if (mParams != null) {
mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
mBackgroundView.setLayoutParams(mParams);
}
mParams = (LayoutParams) mTextView.getLayoutParams();
if (mParams != null) {
mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
mTextView.setLayoutParams(mParams);
}
}
public void setCustomListener(CustomListener l) {
customListener = l;
}
@Override
public void onClick(View v) {
if (customListener != null) {
customListener.onCuscomClick(v, mCustomId);
}
}
public interface CustomListener {
void onCuscomClick(View v, int custom_id);
}
}
代碼很簡單,就不多說,下面來看看我們的CustomView是怎麼用的,請看:
3、自定義控制項的使用
話不多說,請看代碼,main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:nanlus="http://schemas.android.com/apk/res/com.nanlus.custom"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="horizontal" >
<com.nanlus.custom.CustomView
android:id="@+id/custom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
nanlus:background="@drawable/background"
nanlus:custom_id="1"
nanlus:src="@drawable/style_button"
nanlus:text="按鈕1" >
</com.nanlus.custom.CustomView>
</LinearLayout>
</RelativeLayout>
在這里需要解釋一下,
xmlns:nanlus="http://schemas.android.com/apk/res/com.nanlus.custom"
nanlus為在xml中的前綴,com.nanlus.custom為包名
4、在Activity中,直接上代碼
package com.nanlus.custom;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.nanlus.BaseActivity;
import com.nanlus.custom.R;
import com.nanlus.custom.CustomView.CustomListener;
public class CustomActivity extends BaseActivity implements CustomListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((CustomView) this.findViewById(R.id.custom1)).setCustomListener(this);
}
@Override
public void onCuscomClick(View v, int custom_id) {
switch (custom_id) {
case 1:
Toast.makeText(this, "hello !!!", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
⑩ Android如何自定義LinearLayout
LinearLayout自定義方法有多種:
1、自定義xml布局,然後載入布局,自定義一個View繼承LinearLayout
2、在自定義控制項中聲明它的所有子元素,然後在Layout文件中像使用LinearLayout一樣去進行布局。
第二種比較煩 ,它需要在Layout文件中定義好子元素之後,要在代碼 onFinishInflate() 進行匹配子元素。
我就說說載入布局文件的方法吧。
首先:定義好layout文件
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="5dip"
android:paddingLeft="40dip"
android:paddingTop="5dip"
android:src="@drawable/right_icon"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dip"
android:text="主題"
android:textColor="#000000"/>
<LinearLayout
android:layout_width="100dp"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="5dip"
android:paddingLeft="12dip"
android:paddingTop="5dip"
android:src="@drawable/home_icon"/>
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="5dip"
android:paddingLeft="12dip"
android:paddingTop="5dip"
android:src="@drawable/add_icon"/>
</LinearLayout>
</LinearLayout>
{
privateImageViewimageView,iv_home,iv_add;
privateTextViewtextView;
publicMyLinearLayout(Contextcontext){
super(context);
//TODOAuto-generatedconstructorstub
}
publicMyLinearLayout(Contextcontext,AttributeSetattrs){
super(context,attrs);
//TODOAuto-generatedconstructorstub
LayoutInflaterinflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.actionbar,this);
imageView=(ImageView)findViewById(R.id.imageView1);
iv_home=(ImageView)findViewById(R.id.imageView2);
iv_add=(ImageView)findViewById(R.id.imageView3);
textView=(TextView)findViewById(R.id.textView1);
}
/**
*設置圖片資源
*/
publicvoidsetImageResource(intresId){
imageView.setImageResource(resId);
}
/**
*設置顯示的文字
*/
publicvoidsetTextViewText(Stringtext){
textView.setText(text);
}
}
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<cn.com.demo.view.MyLinearLayout
android:id="@+id/ll_actionbar"
android:layout_height="fill_parent<spanstyle="font-family:Tahoma,'MicrosoftYahei',Simsun;">"</span>
android:layout_width="wrap_content"
android:background="@drawable/bg"
/>
</LinearLayout>
接下來自定義一個MyLinearLayout繼承LinearLayout,並且載入剛剛寫好的layout文件。(比如http://www.tiecou.com)
{
privateImageViewimageView,iv_home,iv_add;
privateTextViewtextView;
publicMyLinearLayout(Contextcontext){
super(context);
//TODOAuto-generatedconstructorstub
}
publicMyLinearLayout(Contextcontext,AttributeSetattrs){
super(context,attrs);
//TODOAuto-generatedconstructorstub
LayoutInflaterinflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.actionbar,this);
imageView=(ImageView)findViewById(R.id.imageView1);
iv_home=(ImageView)findViewById(R.id.imageView2);
iv_add=(ImageView)findViewById(R.id.imageView3);
textView=(TextView)findViewById(R.id.textView1);
}
/**
*設置圖片資源
*/
publicvoidsetImageResource(intresId){
imageView.setImageResource(resId);
}
/**
*設置顯示的文字
*/
publicvoidsetTextViewText(Stringtext){
textView.setText(text);
}
}
最後,要的時候使用定義好的MyLinearLayout控制項。