android自定義控制項視頻
① 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自定義控制項,為何要用super將參數context和attrs傳遞給父類的構造函數
這個問題很基礎的吧,面向對象有參構造函數,super,this,建議去一些培訓機構發的視頻,我不是打廣告的哈,b站上就有免費的,我估計你有些東西自己還沒明白
首先如果android開發,自定義View必須直接繼承或者間接繼承View,ViewGroup,
現在想想如果你不加super,那麼你的構造方法等於說什麼有實際意義的代碼都沒有被系統調用
,也就是說你寫的自定義View就是一個空殼子,只不過名字叫做XXX View,構造方法裡面裡面是要初始化數據的,你沒有加super就是你沒有用父類的方法來初始化數據,當然你也可以自己初始化數據,不過沒人這么干,因為麻煩,或者說浪費你自己的時間,畢竟父類已經把一些簡單的初始化代碼已經有了,你自己寫不是給自己找不自在嗎
③ android studio自定義的控制項怎麼使用
首先你要自定義一個控制項,一般採用繼承原有控制項的方式,然後在布局文件使用你要用的自定義控制項,需要包含包名,再就是在activity中寫控制項的控制代碼。
④ android 自定義控制項怎麼實現動畫效果
Google都幫你實現好了,android 5.0上提供了一個新的屬性android:attr/colorControlHighlight,使用這個屬性定義一個ripple_drawable.xml然後在你需要實現水波紋效果的控制項的地方設置這個xml為背景即可。此屬性支持5.0以上設備,否則會出現應用FC。
⑤ 跪求黑馬程序員46期android視頻自定義控制項源碼
其實,不是一定要看視頻源碼才能學會自定義控制項,多看看大神的代碼,也可以學會的。 學習方法有很多種,只是看你能不能想到了。加油,同行
⑥ android中怎樣實現自定義控制項中的組合控制項
public
class
MyView
extends
View{
//
此處省略
構造方法
private
void
onDraw(Canvas
canvas){
//重寫view的onDraw方法,繪制控制項的樣式
//這里你使用canvas來繪制,你布局中使用這個控制項就是你繪制的樣子
}
//然後你可以定義很多自己的一些方法,用來修改控制項的樣式
//假如你自定義的一個
進度條
的話,就要修改進度條值,你就可以自定義方法,讓實現對象來改變進度值,記得修改後調用validate方法更新顯示。(具體函數記不太清了)
}
大概就是這樣實現的自定義控制項,自定義控制項的話優化是很重要的哦,不然性能會很差。
然後你要使用這個控制項的話,在布局中就需要這樣定義,假如這個自定義控制項類是這樣的:
xxx.xxx.MyView。
則使用時:
⑦ androidstudio 自定義控制項
你的自定義控制項只實現了一個參數的構造方法
View有三個構造方法
java">publicView(Contextcontext)
publicView(Contextcontext,AttributeSetattrs)
publicView(Contextcontext,AttributeSetattrs,intdefStyle)
要在布局中使用自定義控制項,控制項必須實現帶參數AttributeSet的構造方法,實例化布局的時候會調用這個方法去實例化控制項,否則就會報你圖上的錯誤
另外引用自定義控制項的時候必須用包名.類名的方式,否則也會報錯
⑧ android 如何封裝一個自定義的控制項
一般使用繼承就能實現自定義的控制項。android的自定義控制項主要是繼承View或者ViewGroup這2個類及其子類。
控制項是對數據和方法的封裝。控制項可以有自己的屬性和方法。屬性是控制項數據的簡單訪問者。方法則是控制項 的一些簡單而可見的功能。
⑨ Android動態添加自定義控制項(照片裝飾素材)canvas 畫板相關問題!求幫助!
點擊的時候獲取到整個父布局(你添加自定義view的父布局),隱藏或者移除不是點擊的這個組件就好了啊,如果不好確定父布局裡面哪個是點擊的空間,那就給每個控制設置id就可以了
⑩ 如何系統的學習android自定義各種酷炫控制項
首先,為什麼需要自定義View?
1. 現有的View滿足不了你的需求,也沒有辦法從已有控制項派生一個出來;界面元素需要自己繪制。
2. 現有View可以滿足要求,把它做成自定義View只是為了抽象:為這個自定義View提供若干方法,方便調用著操縱View。通常做法是派生一個已有View,或者結合xml文件直接inflate。
目前常用的基本上是第二種方式,這種方式非常簡單,與通常的View使用方法基本相同,但是作用卻異常強大,擁有了這一層抽象,代碼更加整潔也更容易維護,通過抽取自定義View的公共操作方法也減少了冗餘代碼,雖然簡單,但不可忽視。
大多數人感覺神秘的應該是第一種,自繪控制項,完全自定義;但其實這兩種方式歸根結底全部都是自繪;不信你去看看TextView的源碼。只不過通常情況下系統幫我們繪制好了一些控制項給開發者使用;OK,接下來就是一個問題。
在講述之前我還是啰嗦地重申一下,復用已有View是最最常用也最有效的自定義View方式,必須熟練使用。
其次,如何自定義View?
想一下,一個View給用戶最直觀的感知是什麼?靜止的形態和動態的操作。靜止的形態意思就是一個View呈現到用戶眼裡長成啥樣子?動態操作指的是,用戶與View之間可以有哪些交互?點擊滑動View的不同地方會有什麼反應?
1. 靜態
如果一個自定義View的樣式都沒有辦法繪制出來,那麼後續的交互就是空談了;我們一步步分解這個問題。
1.1 你的自定義View分為哪幾個部分?是所有的部分都需要手動繪制還是只有一部分——找出需要完全自定義的部分,其他的部分用已有View實現。
1.2 你的自定義View的每個部分長成什麼樣,佔用多大空間——結合理論知識View的measure過程,比如match_parent, wrap_content結合父View的laout_params參數最終測量大小是多少?
1.3 你的自定義View每個部分擺放在哪?相對位置如何?——View的layout過程。
1.4 你的自定義View那些完全需要手動繪制的部分是什麼樣,如何繪制?
你得學會操縱Canvas,學會2D繪圖,什麼?你跟我說3D,OpenGL?學會這些再說。