当前位置:首页 » 安卓系统 » androidimageview自定义

androidimageview自定义

发布时间: 2022-05-16 00:54:42

① 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控件。

热点内容
c语言求5程序 发布:2025-08-16 22:23:40 浏览:978
在windows10中怎么安装安卓模拟器 发布:2025-08-16 22:22:30 浏览:118
flashcms源码 发布:2025-08-16 21:58:43 浏览:489
数据库订货系统 发布:2025-08-16 21:46:57 浏览:19
c语言画点 发布:2025-08-16 21:21:11 浏览:93
如何将游戏数据从安卓转移到iphone 发布:2025-08-16 20:58:55 浏览:364
多表编译教程 发布:2025-08-16 20:58:54 浏览:609
市民云实名制服务器异常 发布:2025-08-16 20:50:51 浏览:713
目标服务器地址格式错误 发布:2025-08-16 20:40:55 浏览:709
文件夹病毒专杀软件 发布:2025-08-16 20:40:50 浏览:807