android ui

Android ui
一、 图片处理。
a) Drawable、Bitmap、byte[]之间的转换
Drawable → Bitmap

public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(
drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(),drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
//canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}

从资源中获取Bitmap
Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);

Bitmap to Drawable
BitmapDrawable bitmapDrawable = (BitmapDrawable)bitmap;
Drawable drawable = (Drawable)bitmapDrawable;
Bitmap bitmap = new Bitmap (...);
Drawable drawable = new BitmapDrawable(bitmap);

Drawable to Bitmap
1 Drawable d = ImagesList.get(0);
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
2
Bitmap drawableToBitmap(Drawable drawable) {

Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
//canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}


Bitmap → byte[]
private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
byte[] → Bitmap
private Bitmap Bytes2Bimap(byte[] b){
if(b.length!=0){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
else {
return null;
}
}

b) 图片的染色过程的代码片段
private void TintThePicture(int deg, int pich, int picw, Bitmap mBitmap,
View v) {
int[] pix = new int[picw * pich];
mBitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);
int RY, GY, BY, RYY, GYY, BYY, R, G, B, Y;
double angle = (3.14159d * (double) deg) / 180.0d;
int S = (int) (256.0d * Math.sin(angle));
int C = (int) (256.0d * Math.cos(angle));
for (int y = 0; y < pich; y++)
for (int x = 0; x < picw; x++) {
int index = y * picw + x;
int r = (pix[index] >> 16) & 0xff; // 0xff
int g = (pix[index] >> 8) & 0xff;
int b = pix[index] & 0xff;

RY = (70 * r - 59 * g - 11 * b) / 100;
GY = (-30 * r + 41 * g - 11 * b) / 100;
BY = (-30 * r - 59 * g + 89 * b) / 100;
Y = (30 * r + 59 * g + 11 * b) / 100;
RYY = (S * BY + C * RY) / 256;
BYY = (C * BY - S * RY) / 256;
GYY = (-51 * RYY - 19 * BYY) / 100;
R = Y + RYY;
R = (R < 0) ? 0 : ((R > 255) ? 255 : R);
G = Y + GYY;
G = (G < 0) ? 0 : ((G > 255) ? 255 : G);
B = Y + BYY;
B = (B < 0) ? 0 : ((B > 255) ? 255 : B);
pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
}
Bitmap bm = Bitmap.createBitmap(picw, pich, Bitmap.Config.ARGB_8888);
bm.setPixels(pix, 0, picw, 0, 0, picw, pich);

Drawable drawable = new BitmapDrawable(bm);
// Drawable drawable = new BitmapDrawable(mBitmap);

v.setBackgroundDrawable(drawable);
v.invalidate();
mBitmap = bm;
pix = null;
}

c) 图片的翻转

int[] pixs = new int[returnIcon.getIntrinsicHeight() * returnIcon.getIntrinsicWidth()];
bitmap.getPixels(pixs , 0, returnIcon.getIntrinsicWidth(), 0, 0, returnIcon.getIntrinsicWidth(),
returnIcon.getIntrinsicHeight());

Bitmap bitmaps = Bitmap.createBitmap(
returnIcon.getIntrinsicWidth(),
returnIcon.getIntrinsicHeight(),
returnIcon.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888: Bitmap.Config.ARGB_8888);

for (int yy = 0 ; yy < returnIcon.getIntrinsicHeight() ; yy++){
bitmaps.setPixels(pixs , yy * returnIcon.getIntrinsicWidth() , returnIcon.getIntrinsicWidth(),0, returnIcon.getIntrinsicHeight() - yy - 1, returnIcon.getIntrinsicWidth() , 1);
}

d) 图片透明度的变化


int alpha = 0x00000000;
各参数的意思:ox表示十六进制。后面依次是argb a:表似乎透明度00表示全透明,ff表示不透明。rgb表示颜色。且0表示完全透明 255表示完全不透明。

mBitmap.getPixels (pix, 0, w, 0, 0, w, h);

for (int y = 0; y<h; y++) {
for (int x=0; x<w; x++) {
int index = y * w + x;
int r = (pix[index] >> 16) & 0xff;
int g = (pix[index] >> 8) & 0xff;
int b = pix[index] & 0xff;

pix[index] = alpha | (r<<16) | (g<<8) | b;
}
alpha = alpha + 0x01000000;

}

e) 倒影的实现
2、
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){
final int reflectionGap = 4;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionImage = Bitmap.createBitmap(bitmap,0, height/2, width, height/2, matrix, false);
Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap, 0, 0, null);
Paint deafalutPaint = new Paint();
canvas.drawRect(0, height,width,height + reflectionGap,deafalutPaint);
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0,
bitmap.getHeight(), 0, bitmapWithReflection.getHeight()+ reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
paint.setShader(shader);
// Set the Transfer mode to be porter duff and destination in
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
// Draw a rectangle using the paint with our linear gradient
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()+ reflectionGap, paint);
return bitmapWithReflection;
}
1.
/**
* 图片倒影的实现
* 像素的操作
* @param srcBitmap
* @return
*/
private Bitmap makeReflectionBitmap(Bitmap srcBitmap) {
int bmpWidth = srcBitmap.getWidth();
int bmpHeight = srcBitmap.getHeight();
int[] pixels = new int[bmpWidth * bmpHeight * 4];
srcBitmap.getPixels(pixels, 0, bmpWidth, 0, 0, bmpWidth, bmpHeight);

// get reversed bitmap
Bitmap reverseBitmap = Bitmap.createBitmap(bmpWidth, bmpHeight,
Bitmap.Config.ARGB_8888);
/**
* 图片的翻转
*/
for (int y = 0; y < bmpHeight; y++) {
reverseBitmap.setPixels(ixels, y * bmpWidth, bmpWidth, 0,
bmpHeight - y - 1, bmpWidth, 1);
}

/**
* 图片透明度的渐变
*/
reverseBitmap.getPixels(pixels, 0, bmpWidth, 0, 0, bmpWidth, bmpHeight);
Bitmap reflectionBitmap = Bitmap.createBitmap(bmpWidth, bmpHeight,
Bitmap.Config.ARGB_8888);
int alpha = 0x00000000;
for (int y = 0; y < bmpHeight; y++) {
for (int x = 0; x < bmpWidth; x++) {
int index = y * bmpWidth + x;
int r = (pixels[index] >> 16) & 0xff;
int g = (pixels[index] >> 8) & 0xff;
int b = pixels[index] & 0xff;

pixels[index] = alpha | (r << 16) | (g << 8) | b;

reflectionBitmap.setPixel(x, y, pixels[index]);
}
alpha = alpha + 0x01000000;
}

return reflectionBitmap;
}

Android Bitmap和Canvas
见examples_05_07 --------- 见examples_05_011
从资源中获取Bitmap
Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);

// 读取InputStream并得到位图
InputStream is=res.openRawResource(R.drawable.pic180);
BitmapDrawable bmpDraw=new BitmapDrawable(is);
Bitmap bmp=bmpDraw.getBitmap();

BitmapDrawablebmpDraw=(BitmapDrawable)res.getDrawable(R.drawable.pic180);
Bitmap bmp=bmpDraw.getBitmap();

使用BitmapFactory获取位图
使用BitmapFactory类decodeStream(InputStream is)解码位图资源,获取位图。
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180);
BitmapFactory的所有函数都是static,这个辅助类可以通过资源ID、路径、文件、数据流等方式来获取位图。
以上方法在编程的时候可以自由选择,在Android SDK中说明可以支持的图片格式如下:png (preferred), jpg (acceptable), gif (discouraged),和bmp(Android SDK Support Media Format)。

2. 获取位图的信息
要获取位图信息,比如位图大小、像素、density、透明度、颜色格式等,获取得到Bitmap就迎刃而解了,这些信息在Bitmap的手册中,这里只是辅助说明以下2点:在Bitmap中对RGB颜色格式使用Bitmap.Config定义,仅包括ALPHA_8、ARGB_4444、ARGB_8888、RGB_565,缺少了一些其他的,比如说RGB_555,在开发中可能需要注意这个小问题;
Bitmap还提供了compress()接口来压缩图片,不过AndroidSAK只支持PNG、JPG格式的压缩;其他格式的需要Android开发人员自己补充了。

3. 显示位图
见examples_05_07
显示位图可以使用核心类Canvas,通过Canvas类的drawBirmap()显示位图,或者借助于BitmapDrawable来将Bitmap绘制到Canvas。当然,也可以通过BitmapDrawable将位图显示到View中。
转换为BitmapDrawable对象显示位图
// 获取位图
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180);
// 转换为BitmapDrawable对象
BitmapDrawable bmpDraw=new BitmapDrawable(bmp);
// 显示位图
ImageView iv2 = (ImageView)findViewById(R.id.ImageView02);
iv2.setImageDrawable(bmpDraw);
使用Canvas类显示位图
这儿采用一个继承自View的子类Panel,在子类的OnDraw中显示
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new Panel(this));
}

class Panel extends View{
public Panel(Context context) {
super(context);
}
public void onDraw(Canvas canvas){
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(bmp, 10, 10, null);
}
}
}
4. 位图缩放
见examples_05_09
(1)将一个位图按照需求重画一遍,画后的位图就是我们需要的了,与位图的显示几乎一样:drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)。
(2)在原有位图的基础上,缩放原位图,创建一个新的位图:CreateBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
(3)借助Canvas的scale(float sx, float sy) (Preconcat the current matrix with the specified scale.),不过要注意此时整个画布都缩放了。
(4)借助Matrix:
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);
Matrix matrix=new Matrix();
matrix.postScale(0.2f, 0.2f);
Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),
bmp.getHeight(),matrix,true);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(dstbmp, 10, 10, null);
5. 位图旋转
见examples_05_08
同样,位图的旋转也可以借助Matrix或者Canvas来实现。
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);
Matrix matrix=new Matrix();
matrix.postScale(0.8f, 0.8f);
matrix.postRotate(45);
Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),
bmp.getHeight(),matrix,true);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(dstbmp, 10, 10, null);
旋转效果:



6.图片水印的生成方法
生成水印的过程。其实分为三个环节:第一,载入原始图片;第二,载入水印图片;第三,保存新的图片。
/**
* create the bitmap from a byte array
*
* @param src the bitmap object you want proecss
* @param watermark the water mark above the src
* @return return a bitmap object ,if paramter's length is 0,return null
*/
private Bitmap createBitmap( Bitmap src, Bitmap watermark )
{
String tag = "createBitmap";
Log.d( tag, "create a new bitmap" );
if( src == null )
{
return null;
}
int w = src.getWidth();
int h = src.getHeight();
int ww = watermark.getWidth();
int wh = watermark.getHeight();
//create the new blank bitmap
Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );//创建一个新的和SRC长度宽度一样的位图
Canvas cv = new Canvas( newb );
//draw src into
cv.drawBitmap( src, 0, 0, null );//在 0,0坐标开始画入src
//draw watermark into
cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角画入水印
//save all clip
cv.save( Canvas.ALL_SAVE_FLAG );//保存
//store
cv.restore();//存储
return newb;
}
7.Canvas的save和restore
onDraw方法会传入一个Canvas对象,它是你用来绘制控件视觉界面的画布。
在onDraw方法里,我们经常会看到调用save和restore方法,它们到底是干什么用的呢?
❑ save:用来保存Canvas的状态。save之后,可以调用Canvas的平移、放缩、旋转、错切、裁剪等操作。
❑ restore:用来恢复Canvas之前保存的状态。防止save后对Canvas执行的操作对后续的绘制有影响。
save和restore要配对使用(restore可以比save少,但不能多),如果restore调用次数比save多,会引发Error。save和restore之间,往往夹杂的是对Canvas的特殊操作。
例如:我们先想在画布上绘制一个右向的三角箭头,当然,我们可以直接绘制,另外,我们也可以先把画布旋转90°,画一个向上的箭头,然后再旋转回来(这种旋转操作对于画圆周上的标记非常有用)。然后,我们想在右下角有个20像素的圆,那么,onDraw中的核心代码是:
int px = getMeasuredWidth();
int py = getMeasuredWidth();
// Draw background
canvas.drawRect(0, 0, px, py, backgroundPaint);
canvas.save();
canvas.rotate(90, px/2, py/2);
// Draw up arrow
canvas.drawLine(px / 2, 0, 0, py / 2, linePaint);
canvas.drawLine(px / 2, 0, px, py / 2, linePaint);
canvas.drawLine(px / 2, 0, px / 2, py, linePaint);
canvas.restore();
// Draw circle
canvas.drawCircle(px - 10, py - 10, 10, linePaint);
效果如图1所示:

如果我们不调用save和restore会是什么样子呢?如图2所示:

从这两个图中,我们就能看到圆圈位置的明显差异。不进行Canvas的save和restore操作的话,所有的图像都是在画布旋转90°后的画布上绘制的。当执行完onDraw方法,系统自动将画布恢复回来。save和restore操作执行的时机不同,就能造成绘制的图形不同

8.Canvas的translate和scale
canvas.translate(X, y);//将坐标体系原点放到(x,y)的位置。
canvas.scale(1.0f, -1.0f);//将图片颠倒过来。

Android下基于XML的Graphics shape使用方法
文章分类:移动开发
Android下基于XML的 Graphics

以前作图,一般有两种方式,首先是UI把图形设计好,我们直接贴,对于那些简单的图形,如矩形、扇形这样的图
形,一般的系统的API会提供这样的接口,但是在Android下,有第三种画图方式,介于二者之间,结合二者的长处,如
下的代码:
Java 代码
<item android:id="@android:id/secondaryProgress">
<clip>

<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#0055ff88"
android:centerColor="#0055ff00"
android:centerY="0.75"
android:endColor="#00320077"
android:angle="270"
/>
</shape>
</clip>
</item>
这是一个Progress的style里面的代码,描述的是进度条的为达到的图形,原本以为这是一个图片,后来仔细的跟踪代码,
发现居然是 xml,像这种shape corners gradient等等这还是第一次碰到。shape 表示是一个图形,corners表示是有半径
为5像素的圆角,然后,gradient表示一个渐变。这样作图简单明了,并且可以做出要求很好的图形,并且节省资源

Java 代码
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF"
android:angle="270"/>
<padding android:left="50dp" android:top="20dp"
android:right="7dp" android:bottom="7dp" />
<corners android:radius="8dp" />
</shape>
gradient 产生颜色渐变 android:angle 从哪个角度开始变貌似只有90的整数倍可以
android:shape="rectangle" 默认的也是长方形

Java 代码
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="#ff4100ff"/>
<stroke android:width="2dp" android:color="#ee31ff5e"
android:dashWidth="3dp" android:dashGap="2dp" />
<padding android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="7dp" />
<corners android:radius="6dp" />
</shape>
#ff4100ff蓝色#ff4100ff绿色
<solid android:color="#ff4100ff"/>实心的填充里面
<stroke 描边采用那样的方式将外形轮廓线画出来
android:dashWidth="3dp" android:dashGap="2dp" 默认值为0
android:width="2dp" android:color="#FF00ff00"笔的粗细,
android:dashWidth="5dp" android:dashGap="5dp" 实现- - -这样的效果,dashWidth指的是一条小横线的宽度
dashGap 指的是小横线与小横线的间距。 width="2dp" 不能太宽


shape等特殊xml

1.用 shape 作为背景
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#f0600000"/>
<stroke android:width="3dp" color="#ffff8080"/>
<corners android:radius="3dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
一定要注意solid android:color="#f0600000" 是背景色要用8位最好不要完全透明不然没有效果啊这句话本来就不
是背景色的意思

2.类似多选的效果:
(1) listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

listView.setItemsCanFocus(false);
(2) define list item
CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:background="@drawable/txt_view_bg"/>
(3) define drawable txt_view_bg.xml <item android:drawable="@drawable/selected" android:state_checked="true" /><item android:drawable="@drawable/not_selected" />

3.
<LinearLayout android:layout_width ="100dp" android:layout_height="wrap_content" />
LinearLayour ll = new LinearLayout(this);parentView.addView(ll, new LinearLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT));

4. 当设置 TextView setEnabled(false)时背景颜色你如果用#ffff之类的话可能不会显示你最好使用 android:textColor这个属性而不是使用color。
<TextView android:text="whatever text you want" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/example" />

res/color/example.xml):

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="false" android:color="@color/disabled_color" /> <item android:color="@color/normal_color"/></selector>

http://developer.android.com/intl/zh-CN/reference/android/content/res/ColorStateList.html

5.
http://android.amberfog.com/?p=9
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#FFF8F8F8" />
</shape>
</item>
<item android:top="23px">
<shape>
<solid android:color="#FFE7E7E8" />
</shape>
</item>
</layer-list>
You can simple combine several drawables into one using <layer-list> tag.
note: Unfortenately you cannot resize drawables in layer-list. You can only move it.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/shape_below"/> <item android:top="10px" android:right="10px" android:drawable="@drawable/shape_cover"/></layer-list>


include
You can put similar layout elements into separate XML and use <include> tag to use it.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="64dip"
android:gravity="center_vertical"
android:ignoreGravity="@+id/icon">

<include layout="@layout/track_list_item_common" />;

</RelativeLayout>

track_list_item_common.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android">

<ImageView android:id="@+id/icon"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="4dip"
android:layout_width="60px"
android:layout_height="60px"/>
...
</merge>

Android特殊用法收集:
1.让一个图片透明:
Bitmap buffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);buffer.eraseColor(Color.TRANSPARENT);

2.直接发送邮件:
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri .fromParts("mailto", "test@test.com", null));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

3.程序控制屏幕变亮:
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 100 / 100.0f;
getWindow().setAttributes(lp);

4.过滤特定文本
Filter filter = myAdapter.getFilter();
filter.filter(mySearchText);

5scrollView scroll停止事件
setOnScrollListener(new OnScrollListener(){
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub }
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
if(scrollState == 0) Log.i("a", "scrolling stopped..."); } });}

6. 对于特定的程序发起一个关联供打开
Bitmap bmp = getImageBitmap(jpg);
String path = getFilesDir().getAbsolutePath() + "/test.png";
File file = new File(path);
FileOutputStream fos = new FileOutputStream(file);
bmp.compress( CompressFormat.PNG, 100, fos );
fos.close();

Intent intent = new Intent();
intent.setAction(android .content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri .fromFile(new File(path)), "image/png");
startActivity(intent);
对于图片上边的不适用索引格式会出错。
Intent intent = new Intent();
intent.setAction(android .content.Intent.ACTION_VIEW);
File file = new File("/sdcard/test.mp4");
intent.setDataAndType(Uri .fromFile(file), "video/*");
startActivity(intent);

Intent intent = new Intent();
intent.setAction(android .content.Intent.ACTION_VIEW);
File file = new File("/sdcard/test.mp3");
intent.setDataAndType(Uri .fromFile(file), "audio/*");
startActivity(intent);


7.设置文本外观
setTextAppearance(context, android .R.style.TextAppearance_Medium);
android :textAppearance="?android :attr/textAppearanceMedium"

8.设置单独的发起模式:
<activity
android :name=".ArtistActivity"
android :label="Artist"
android :launchMode="singleTop">
</activity>

Intent i = new Intent();
i.putExtra(EXTRA_KEY_ARTIST, id);
i.setClass(this, ArtistActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
9.创建一个圆角图片
这个的主要原理其实就是利用遮罩,先创建一个圆角方框然后将图片放在下面:
Bitmap myCoolBitmap = ... ;
int w = myCoolBitmap.getWidth(), h = myCoolBitmap.getHeight();
Bitmap rounder = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(rounder);
Paint xferPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
xferPaint.setColor(Color.RED);
canvas.drawRoundRect(new RectF(0,0,w,h), 20.0f, 20.0f, xferPaint);
xferPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

然后呢实现
canvas.drawBitmap(myCoolBitmap, 0,0, null);
canvas.drawBitmap(rounder, 0, 0, xferPaint);

10 在notification 上的icon上加上数字给人提示有多少个未读
Notification notification = new Notification (icon, tickerText, when);
notification .number = 4;

11背景渐变:
首先建立文件drawable/shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android ="http://schemas.android .com/apk/res/android " android :shape="rectangle">
<gradient android :startColor="#FFFFFFFF" android :endColor="#FFFF0000"
android :angle="270"/>
</shape>
在该文件中设置渐变的开始颜色(startColor)、结束颜色(endColor)和角度(angle)

接着创建一个主题values/style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="NewTheme" parent="android :Theme">
<item name="android :background">@drawable/shape</item>
</style>
</resources>

然后在AndroidManifest.xml文件中的application或activity中引入该主题,如:
<activity android :name=".ShapeDemo" android :theme="@style/NewTheme">

该方法同样适用于控件 http://17f8.cn/trackback.php?tbID=259&extra=9d45e9

12. 储存数据当你在一个实例中保存静态数据,此示例关闭后下一个实例想引用静态数据就会为null,这里呢必须重写applition
public class MyApplication extends Application{
private String thing = null;
public String getThing(){
return thing;
}
public void setThing( String thing ){
this.thing = thing; }
}
public class MyActivity extends Activity {
private MyApplication app;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = ((MyApplication)getApplication());
String thing = app.getThing();
}
}

RatingBar自带的样式
<RatingBarandroid:id="@+id/rating_bar"
android:layout_alignBottom="@+id/item_info_02"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall">

改变ListView拖动时有黑色的阴影
文章分类:移动开发
改变ListView拖动有黑影的方法:
1. 代码中:listView.setCacheColorHint(0);
2. XML布局文件中:android:cacheColorHint="#00000000"

关于透明色的问题还是存在疑问:
『转』
<color name = "transparent_background"> #50000000 </color>
#5000000前两位是透明的效果参数从00--99(透明--不怎么透明),后6位是颜色的设置

Selector的使用
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false"
android:drawable="@drawable/没有焦点时的图片背景" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable=
"@drawable/非触摸模式下获得焦点并单击时的背景图片" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/触摸模式下单击时的背景图片" />
<item android:state_selected="true"
android:drawable="@drawable/选中时的图片背景" />
<item android:state_focused="true"
android:drawable="@drawable/获得焦点时的图片背景" />
</selector>


Eg:<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/bottom_jingpin_unselected" />
<item android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/bottom_jingpin_unselected" />
<item android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/bottom_jingpin" />
<item android:state_pressed="true" android:drawable="@drawable/bottom_jingpin" />
<item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/bottom_jingpin_unselected" />
<item android:state_enabled="false" android:drawable="@drawable/bottom_jingpin" />
<item android:state_focused="true" android:drawable="@drawable/bottom_jingpin_unselected" />
<item android:drawable="@drawable/bottom_jingpin_unselected" />
</selector>
图片处理的一些方法


1.图片加载方法,方便用户加载图片
/***
* 加载本地图片
* @param context:主运行函数实例
* @param bitAdress:图片地址,一般指向R下的drawable目录
* @return
*/
public final Bitmap CreatImage(Context context, int bitAdress) {
Bitmap bitmaptemp = null;
bitmaptemp = BitmapFactory.decodeResource(context.getResources(),
bitAdress);
return bitmaptemp;
}
2.图片平均分割方法,将大图平均分割为N行N列,方便用户使用
/***
* 图片分割
*
* @param g
* :画布
* @param paint
* :画笔
* @param imgBit
* :图片
* @param x
* :X轴起点坐标
* @param y
* :Y轴起点坐标
* @param w
* :单一图片的宽度
* @param h
* :单一图片的高度
* @param line
* :第几列
* @param row
* :第几行
*/
public final void cuteImage(Canvas g, Paint paint, Bitmap imgBit, int x,
int y, int w, int h, int line, int row) {
g.clipRect(x, y, x + w, h + y);
g.drawBitmap(imgBit, x - line * w, y - row * h, paint);
g.restore();
}
3.图片缩放,对当前图片进行缩放处理
/***
* 图片的缩放方法
*
* @param bgimage
* :源图片资源
* @param newWidth
* :缩放后宽度
* @param newHeight
* :缩放后高度
* @return
*/
public Bitmap zoomImage(Bitmap bgimage, int newWidth, int newHeight) {
// 获取这个图片的宽和高
int width = bgimage.getWidth();
int height = bgimage.getHeight();
// 创建操作图片用的matrix对象
Matrix matrix = new Matrix();
// 计算缩放率,新尺寸除原始尺寸
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 缩放图片动作
matrix.postScale(scaleWidth, scaleHeight);
Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, width, height,
matrix, true);
return bitmap;
}
4.绘制带有边框的文字,一般在游戏中起文字的美化作用
/***
* 绘制带有边框的文字
*
* @param strMsg
* :绘制内容
* @param g
* :画布
* @param paint
* :画笔
* @param setx
* ::X轴起始坐标
* @param sety
* :Y轴的起始坐标
* @param fg
* :前景色
* @param bg
* :背景色
*/
public void drawText(String strMsg, Canvas g, Paint paint, int setx,
int sety, int fg, int bg) {
paint.setColor(bg);
g.drawText(strMsg, setx + 1, sety, paint);
g.drawText(strMsg, setx, sety - 1, paint);
g.drawText(strMsg, setx, sety + 1, paint);
g.drawText(strMsg, setx - 1, sety, paint);
paint.setColor(fg);
g.drawText(strMsg, setx, sety, paint);
g.restore();
}

RatingBar)的使用
● 评分(星)的最大数的设置,调用setNumStars()方法。
● 现在的评分的设置,调用setRating()方法。


界面设置:
1. //隐藏标题栏
2. requestWindowFeature(Window.FEATURE_NO_TITLE);
3. //设置成全屏
4. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
5. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值