101)圖片去色,返回灰度圖片
102)把圖片變成圓角
103)在代碼中從資源文件中獲取一張Bitmap圖片
104)自定義組件屬性時(shí),命名空間可以這樣寫(xiě)
105)ic_lunach尺寸設(shè)置
106) android軟鍵盤以及輸入法影響界面布局的問(wèn)題
107) 給activity設(shè)置字體默認(rèn)顏色和字體大小
108) 剪切圖片
109) 獲取當(dāng)前線程名稱
110) 代碼中設(shè)置view的寬高
111) SharedPreferences的改變監(jiān)聽(tīng)
112) 關(guān)于屏幕距離的一些知識(shí)
113) listView隔行設(shè)置顏色
114) 根據(jù)橫豎屏不同狀態(tài) 設(shè)置不同的顯示界面
115) 保持界面常亮
116) 延遲主線程的某種操作
117) 獲取網(wǎng)絡(luò)頁(yè)面資源流
118) Listview和RadioButton一起使用時(shí)
119) edittext可以錄入浮點(diǎn)型
120) 有下標(biāo)的選擇
121) 利用ContentProvider掃描手機(jī)中所有圖片
122) 點(diǎn)擊Listview的item項(xiàng)嚣州,沒(méi)有反應(yīng)問(wèn)題
123) 如果當(dāng)前activity的啟動(dòng)模式是singleTask...則它上個(gè)界面給它傳來(lái)的bundle都不能接收到
124) 單例模式模板
125) 在組件四周添加圖片的方法
126) 選擇器屬性詳解
127) seekBar組件的樣式
128) 給PopupWindow設(shè)置動(dòng)畫(huà)效果
129) 16進(jìn)制顏色設(shè)置
130) 在一個(gè)程序中啟動(dòng)另一個(gè)程序
131) 讓自己的應(yīng)用也成為系統(tǒng)照相機(jī)
132) 存放數(shù)據(jù)到sd卡匕得,不同方法的區(qū)別
133) activity系統(tǒng)自動(dòng)動(dòng)畫(huà)
134) 動(dòng)態(tài)創(chuàng)建組件到布局
135)判斷耳機(jī)孔是否被插入
136) Radiogroup中的radioButton都不可編輯方法
137) 防止按鈕連續(xù)點(diǎn)擊
138) 系統(tǒng)默認(rèn)參數(shù)
139) 去除默認(rèn)GridView的邊距
140) dp和px直接轉(zhuǎn)換方法
141) style添加自定義組件屬性值
142) Fragment使用方法
143) list 的 遍歷方式
144) 使用系統(tǒng)相機(jī)和系統(tǒng)相冊(cè)獲取圖片
145) 將圖片保存到sd卡文件夾下后咽筋,圖庫(kù)無(wú)法顯示的問(wèn)題
146) listview中的一些常見(jiàn)大坑
147) Handler的靜態(tài)使用
- 網(wǎng)絡(luò)連接超時(shí)設(shè)置
149) List 去除元素的方法
150) 流轉(zhuǎn)轉(zhuǎn)化成字符串
101)圖片去色,返回灰度圖片
/**
* 圖片去色,返回灰度圖片
*
* @param bmpOriginal 傳入的圖片
* @return 去色后的圖片
*/
public static Bitmap toGrayscale(Bitmap bmpOriginal) {
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0, 0, paint);
return bmpGrayscale;
}
102)把圖片變成圓角
public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap
.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
103)在代碼中從資源文件中獲取一張Bitmap圖片
Bitmap dragBitmap = BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.getup_slider_ico_normal);
104)自定義組件屬性時(shí)曙博,命名空間可以這樣寫(xiě):
xmlns:ProgressWheel="http://schemas.android.com/apk/res-auto"
105)ic_lunach尺寸設(shè)置
m: 48*48 h:72*72 xh: 96*96 xxh: 144*144
106) android軟鍵盤以及輸入法影響界面布局的問(wèn)題
在androidMainfest.xml文件中在此Activity中寫(xiě)入 android:windowSoftInputMode="adjustPan"
107) 給activity設(shè)置字體默認(rèn)顏色和字體大小:
styles.xml
<style name="AppTheme" parent="@android:style/Theme.Light.NoTitleBar">
<item name="android:textSize">42dp</item>
<item name="android:textColor">#FF0000</item>
</style>
AndroidManifest.xml文件中引用主題
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
108) 剪切圖片
res/drawable/drawable_clip.xml
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/phone2"
android:clipOrientation="vertical"
android:gravity="top">
</clip>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/drawable_clip" />
mageView=(ImageView)this.findViewById(R.id.imageView1);
mDownloadProgressDrawable = (ClipDrawable)imageView.getDrawable();
mDownloadProgressDrawable.setLevel(100*50); //0-10000
109) 獲取當(dāng)前線程名稱:
Thread.currentThread().getName()淮腾; 如果是main 的話昌犹,就是主進(jìn)程
110)代碼中設(shè)置view的寬高
LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) mInfo.getLayoutParams();
linearParams.height = 800;
mInfo.setLayoutParams(linearParams);
111) SharedPreferences的改變監(jiān)聽(tīng)
sp=getSharedPreferences("key",Context.MODE_PRIVATE);
sp.registerOnSharedPreferenceChangeListener(this);
112) 關(guān)于屏幕距離的一些知識(shí)
標(biāo)題欄:48dp
左右邊距:16dp
兩個(gè)組件的邊距:8dp
字體大屑嵛摺:14(small),18(middle),22(L)
laular圖標(biāo):m(160dpi):48*48 h(240dpi):72*72 x(320dpi): 96*96 xx(480dpi): 144*144 xxx:192*192
113) listView隔行設(shè)置顏色
private int[] colors = new int[] { 0x30FF0000, 0x300000FF };
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
return view;
}
114) 根據(jù)橫豎屏不同狀態(tài) 設(shè)置不同的顯示界面
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// 橫屏
setContentView(R.layout.main_l);
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// 豎屏
setContentView(R.layout.main);
}
115) 保持界面常亮
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
116) 延遲主線程的某種操作
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
MCIntent.sendIntent(PatientAddActivity1.this, PatientDataActivity.class,"patientInfo",mPatientInfo);
MCProgress.dismiss();
finish();
}
}, 1000);
117) 獲取網(wǎng)絡(luò)頁(yè)面資源流
try {
URL url=new URL("http://123.57.14.154:28080/MPD/consultation/consultationListPre.do");
System.out.println("協(xié)議:"+url.getProtocol());
System.out.println("域名:"+url.getHost());
System.out.println("端口:"+url.getPort());
System.out.println("資源:"+url.getFile());
System.out.println("相對(duì)路徑:"+url.getFile());
System.out.println("錨點(diǎn):"+url.getRef());
System.out.println("參數(shù):"+url.getQuery());
//1.獲取資源
InputStream is = url.openStream();
byte[] flush=new byte[1024];
int len=0;
while((len=is.read(flush)) != -1){
System.out.println(new String(flush,0,len));
}
is.close();
//2.另一種獲取資源 可以改變編碼
BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream(),"utf-8"));
String msg=null;
while((msg=br.readLine())!=null){
System.out.println(msg);
}
br.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
118) Listview和RadioButton一起使用時(shí)
RadioButton設(shè)置 android:focusable="false"
119) edittext可以錄入浮點(diǎn)型
view_conent2_ed.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(3)});
view_conent_ed.setInputType(InputType.TYPE_CLASS_NUMBER| InputType.TYPE_NUMBER_FLAG_DECIMAL);
view_conent_ed.setFilters(new InputFilter[]{new InputFilter.LengthFilter(conentLength)});
120) 有下標(biāo)的選擇
private void initImageLine() {
image = (ImageView) findViewById(R.id.cursor);
bmpW = BitmapFactory.decodeResource(getResources(),
R.drawable.line_blue).getWidth();
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenW = dm.widthPixels;
offset = (screenW / 4- bmpW) / 2; //4是將屏幕分成4分
// imgageview設(shè)置平移,使下劃線平移到初始位置(平移一個(gè)offset)
Matrix matrix = new Matrix();
matrix.postTranslate(offset, 0);
image.setImageMatrix(matrix);
moblieLeng = offset*2 + bmpW;// 兩個(gè)相鄰頁(yè)面的偏移量
}
/這個(gè)方法在
viewpager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int postion) {
animation(postion);
}
使用
private void animation(int postion) {
Animation animation = new TranslateAnimation(currIndex * moblieLeng, postion * moblieLeng, 0, 0);// 平移動(dòng)畫(huà)
currIndex = postion;
animation.setFillAfter(true);// 動(dòng)畫(huà)終止時(shí)停留在最后一幀斜姥,不然會(huì)回到?jīng)]有執(zhí)行前的狀態(tài)
animation.setDuration(200);// 動(dòng)畫(huà)持續(xù)時(shí)間0.2秒
image.startAnimation(animation);// 是用ImageView來(lái)顯示動(dòng)畫(huà)的
}
121) 利用ContentProvider掃描手機(jī)中所有圖片
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
private void getReasourcePic() {
// 判斷sd卡是否可用
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
Toast.makeText(this, "當(dāng)前存儲(chǔ)卡不可用!", Toast.LENGTH_LONG).show();
return;
}
// ProgressDialog mProgressDialog = ProgressDialog.show(this, null,
// "正在加載....");
// 開(kāi)啟線程掃描
new Thread() {
public void run() {
// 所有圖片的url
Uri mImgUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver cr = MainActivity.this.getContentResolver();
// 根據(jù)圖片更改的時(shí)間排序獲取圖片
Cursor cursor = cr.query(mImgUri, null,
MediaStore.Images.Media.MIME_TYPE + " =? or "
+ MediaStore.Images.Media.MIME_TYPE + " =? ",
new String[] {"image/jpeg","image/png"},
MediaStore.Images.Media.DATE_MODIFIED);
Set<String> mDirPaths = new HashSet<String>(); // 防止重復(fù)遍歷使用的集合
// 獲取的文件可能會(huì)都在一個(gè)文件夾下
while (cursor.moveToNext()) {
// 獲取圖片路徑
String path = cursor.getString(cursor
.getColumnIndex(MediaStore.Images.Media.DATA));
// 根據(jù)圖片的路徑獲取文件夾
File parentFile = new File(path).getParentFile();
if (parentFile == null)
continue;
// 獲取文件夾的路徑
String dirPath = parentFile.getAbsolutePath();
// 集合中是否存在該文件夾
if (mDirPaths.contains(dirPath)) {
continue;
} else {
mDirPaths.add(dirPath); // 不存在就放入集合中
// bean.setDir(dirPath);
// bean.setFirstImagePath(path);
Log.i("TAG", dirPath);
}
if (parentFile.list() == null)
continue;
// 獲取文件夾下圖片的數(shù)量
int picSize = parentFile.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
if (filename.endsWith(".jpg")
|| filename.endsWith(".jpeg")
|| filename.endsWith(".png"))
return true;
return false;
}
}).length;
// bean.setCount(picSize); //文件夾下圖片的數(shù)量
// if(picSize > mMaxCount){
// mMaxCount = picSize;
// mCurrentDir=parentFile;
// }
}
cursor.close();
};
}.start();
}
122) 點(diǎn)擊Listview的item項(xiàng)鸿竖,沒(méi)有反應(yīng)問(wèn)題
點(diǎn)擊每一個(gè)item的時(shí)候沒(méi)有反應(yīng)沧竟,無(wú)法獲取的焦點(diǎn)。原因多半是由于在你自己
定義的Item中存在諸如ImageButton缚忧,Button悟泵,CheckBox等子控件,他們獲取了
焦點(diǎn)搔谴,
所以定義viewGroup和其子控件兩者之間的關(guān)系:
android:descendantFocusability
beforeDescendants:viewgroup會(huì)優(yōu)先其子類控件而獲取到焦點(diǎn)
afterDescendants:viewgroup只有當(dāng)其子類控件不需要獲取焦點(diǎn)時(shí)才獲取焦點(diǎn)
blocksDescendants:viewgroup會(huì)覆蓋子類控件而直接獲得焦點(diǎn)
123)如果當(dāng)前activity的啟動(dòng)模式是singleTask...則它上個(gè)界面給它傳來(lái)的bundle都不能接收到
124)單例模式模板
public class ImageLoader {
private static ImageLoader mInstance;
public ImageLoader() {
}
public static ImageLoader getInstance(){
if(mInstance==null){
//當(dāng)兩個(gè)線程同時(shí)到達(dá)時(shí)魁袜,同步處理
synchronized (ImageLoader.class) {
if(mInstance==null){
mInstance=new ImageLoader();
}
}
}
return mInstance;
}
}
125) 在組件四周添加圖片的方法
xml中使用:android:drawableBottom
代碼中使用:setCompoundDrawablesWithIntrinsicBounds()和setCompoundDrawablesWithIntrinsicBounds。
126)選擇器屬性詳解
android:state_pressed 如果是true敦第,當(dāng)被點(diǎn)擊時(shí)顯示該圖片,如果是false沒(méi)被按下時(shí)顯示默認(rèn)
android:state_focused true店量,獲得焦點(diǎn)時(shí)顯示芜果;false,沒(méi)獲得焦點(diǎn)顯示默認(rèn)
android:state_hovered 光標(biāo)是否懸停融师,通常與focused state相同右钾,它是4.0的新特性
android:state_selected true,當(dāng)被選擇時(shí)顯示該圖片旱爆;false舀射,當(dāng)未被選擇時(shí)顯示該圖片。
android:state_checkable true怀伦,當(dāng)CheckBox能使用時(shí)顯示該圖片脆烟;false,當(dāng)CheckBox不能使用時(shí)顯示該圖片房待。
android:state_checked true邢羔,當(dāng)CheckBox選中時(shí)顯示該圖片;false桑孩,當(dāng)CheckBox為選中時(shí)顯示該圖片拜鹤。
android:state_enabled true,當(dāng)該組件能使用時(shí)顯示該圖片流椒;false敏簿,當(dāng)該組件不能使用時(shí)顯示該圖片。
android:state_activated 被激活(這個(gè)麻煩舉個(gè)例子宣虾,不是特明白)
android:state_window_focused true惯裕,當(dāng)此activity獲得焦點(diǎn)在最前面時(shí)顯示該圖片;false安岂,當(dāng)沒(méi)在最前面時(shí)顯示該圖片
127) seekBar組件的樣式
<SeekBar
android:id="@+id/view_seekBar"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:progressDrawable="@drawable/seekbar_style"
android:thumb="@null" />
seekbar_style.xml
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="270"
android:centerColor="#EBEBEB"
android:centerY="0.75"
android:endColor="#EBEBEB"
android:startColor="#EBEBEB" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="270"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:startColor="#80ffd300" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="270"
android:centerColor="@color/main_color"
android:centerY="0.75"
android:endColor="@color/main_color"
android:startColor="@color/main_color" />
</shape>
</clip>
</item>
</layer-list>
128) 給PopupWindow設(shè)置動(dòng)畫(huà)效果
從上到下轻猖,從下到上
mDirPopupWindow.setAnimationStyle(R.style.dir_popup_anim);
<style
name="dir_popup_anim">
<item name="android:windowEnterAnimation">@anim/side_up</item>
<item name="android:windowExitAnimation">@anim/side_down</item>
</style>
side_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fromXDelta="0"
android:fromYDelta="100%"
android:toXDelta="0"
android:toYDelta="0"/>
</set>
side_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="100%"/>
</set>
129) 16進(jìn)制顏色設(shè)置
格式如#00FFFFFF,前兩位代表不透明度的十六進(jìn)制域那。00表示完全透明咙边,F(xiàn)F就是全不透明
100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
50% — 80
45% — 73
40% — 66
35% — 59
30% — 4D
25% — 40
20% — 33
15% — 26
10% — 1A
5% — 0D
0% — 00
130) 在一個(gè)程序中啟動(dòng)另一個(gè)程序
1.知道了另一個(gè)應(yīng)用的包名和MainActivity的名字之后便可以直接通過(guò)如下代碼來(lái)啟動(dòng):
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
startActivity(intent);
2.一般都不知道應(yīng)用程序的啟動(dòng)Activity的類名猜煮,而只知道包名,我們可以通過(guò)ResolveInfo類來(lái)取得啟動(dòng)Acitivty的類名
131) 讓自己的應(yīng)用也成為系統(tǒng)照相機(jī)
僅需在注冊(cè)文件夾主動(dòng)類下败许,新增一個(gè)攔截器:
<intent-filter>
<action android:name="android.media.action.IMAGE_CAPTURE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
132) 存放數(shù)據(jù)到sd卡王带,不同方法的區(qū)別
應(yīng)用程序在運(yùn)行的過(guò)程中如果需要向手機(jī)上保存數(shù)據(jù),一般是把數(shù)據(jù)保存在SDcard中的市殷。
大部分應(yīng)用是直接在SDCard的根目錄下創(chuàng)建一個(gè)文件夾愕撰,然后把數(shù)據(jù)保存在該文件夾中。
這樣當(dāng)該應(yīng)用被卸載后醋寝,這些數(shù)據(jù)還保留在SDCard中搞挣,留下了垃圾數(shù)據(jù)。
Context.getExternalFilesDir():一般放一些長(zhǎng)時(shí)間保存的數(shù)據(jù) SDCard/Android/data/你的應(yīng)用的包名/files/ 目錄
Context.getExternalCacheDir():一般存放臨時(shí)緩存數(shù)據(jù) SDCard/Android/data/你的應(yīng)用包名/cache/目錄
如果使用上面的方法音羞,當(dāng)你的應(yīng)用在被用戶卸載后囱桨,SDCard/Android/data/你的應(yīng)用的包名/ 這個(gè)目錄下的所有文件都會(huì)被刪除,不會(huì)留下垃圾信息嗅绰。
上面二個(gè)目錄分別對(duì)應(yīng) 設(shè)置->應(yīng)用->應(yīng)用詳情里面的”清除數(shù)據(jù)“與”清除緩存“選項(xiàng)
133) activity系統(tǒng)自動(dòng)動(dòng)畫(huà)
overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out); //淡入淡出的效果
overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right); //由左向右滑入的效果
134) 動(dòng)態(tài)創(chuàng)建組件到布局
ListView listView=new ListView(this);
LinearLayout.LayoutParams param=new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
listView.setBackgroundColor(Color.BLACK);
parentLayou.addView(listView,param);
135)判斷耳機(jī)孔是否被插入
AudioManager localAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
localAudioManager.isWiredHeadsetOn() 如果插入了耳機(jī)舍肠,就返回true,否則false;
權(quán)限
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
136) Radiogroup中的radioButton都不可編輯方法
RadioButton.setClickable(false);
137) 防止按鈕連續(xù)點(diǎn)擊
public class CommonUtils {
private static long lastClickTime;
public synchronized static boolean isSlowClick() {
long time = System.currentTimeMillis();
if (time - lastClickTime < 1500) {
return false;
}
lastClickTime = time;
return true;
}
}
if(CommonUtils.isSlowClick()){
}
138) 系統(tǒng)默認(rèn)參數(shù)
Android平臺(tái)定義了三種字體大小:
"?android:attr/textAppearanceLarge"
"?android:attr/textAppearanceMedium"
"?android:attr/textAppearanceSmall"
Android字體顏色:
android:textColor="?android:attr/textColorPrimary"
android:textColor="?android:attr/textColorSecondary"
android:textColor="?android:attr/textColorTertiary"
android:textColor="?android:attr/textColorPrimaryInverse"
android:textColor="?android:attr/textColorSecondaryInverse"
分隔符
橫向:
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="?android:attr/listDivider" />
縱向:
<View android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="?android:attr/listDivider" />
139) 去除默認(rèn)GridView的邊距
android:listSelector="@null" 使用這個(gè)可去除邊距
140) dp和px直接轉(zhuǎn)換方法
public static float applyDimension(int unit, float value,
DisplayMetrics metrics)
{
switch (unit) {
case TypedValue.COMPLEX_UNIT_PX:
return value;
case COMPLEX_UNIT_DIP:
return value * metrics.density;
case COMPLEX_UNIT_SP:
return value * metrics.scaledDensity;
case COMPLEX_UNIT_PT:
return value * metrics.xdpi * (1.0f/72);
case COMPLEX_UNIT_IN:
return value * metrics.xdpi;
case COMPLEX_UNIT_MM:
return value * metrics.xdpi * (1.0f/25.4f);
}
return 0;
}
141) style添加自定義組件屬性值
自定義屬性在styles.xml中都不需要命名空間可以直接用屬性名就可以了
<style name="input_myedit_style">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="titlewidth">@dimen/survery_titlewidth</item>
</style>
142) Fragment使用方法
onCreateView() 方法中只進(jìn)行組件的初始化
if (view == null) {
view = inflater.inflate(R.layout.hello, null);
}
在 onStart() 中初始化數(shù)據(jù)
143) list 的 遍歷方式
144) 使用系統(tǒng)相機(jī)和系統(tǒng)相冊(cè)獲取圖片
(1)相機(jī)獲取圖片
打開(kāi)系統(tǒng)相機(jī):
Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"image.jpg"));
//指定照片保存路徑(SD卡)窘面,image.jpg為一個(gè)臨時(shí)文件翠语,每次拍照后這個(gè)圖片都會(huì)被替換
openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(openCameraIntent, TAKE_PICTURE);
在回調(diào)中獲取圖片:
Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/image.jpg");
iv_image.setImageBitmap(newBitmap);
(2)系統(tǒng)相冊(cè)獲取圖片:
打開(kāi)系統(tǒng)相冊(cè):
Intent openAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT);
openAlbumIntent.setType("image/*");
startActivityForResult(openAlbumIntent, CHOOSE_PICTURE);
在回調(diào)中獲取圖片:
ContentResolver resolver = getContentResolver();
//照片的原始資源地址
Uri originalUri = data.getData();
try {
//使用ContentProvider通過(guò)URI獲取原始圖片
Bitmap photo = MediaStore.Images.Media.getBitmap(resolver, originalUri);
if (photo != null) {
iv_image.setImageBitmap(smallBitmap);
}
(3)修剪圖片
public void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// 設(shè)置裁剪
intent.putExtra("crop", "true");
// aspectX aspectY 是寬高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪圖片寬高
intent.putExtra("outputX", 340);
intent.putExtra("outputY", 340);
intent.putExtra("return-data", true);
startActivityForResult(intent, RESULT_REQUEST_CODE);
}
保存裁剪之后的圖片數(shù)據(jù)
private void getImageToView(Intent data) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
Drawable drawable = new BitmapDrawable(this.getResources(), photo);
iv_photo.setImageDrawable(drawable);
}
}
145) 將圖片保存到sd卡文件夾下后,圖庫(kù)無(wú)法顯示的問(wèn)題
/**
* 使用照相機(jī) 保存圖片到文件夾下财边,刷新圖庫(kù)
* @param context
* @param path
*/
public static void galleryAddPic(Context context, String path) {
Intent mediaScanIntent = new Intent( Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(path);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent); //要想看的該圖片要進(jìn)行廣播
}
146) listview中的一些常見(jiàn)大坑
(1)ViewHolder 是內(nèi)部類肌括,所以最好用靜態(tài)的
static class ViewHolder {。制圈。们童。}
(2)數(shù)據(jù)錯(cuò)亂:
holder.view_name .setTag(holder.view_name.toString());
if(holder.view_name.getTag().equals(holder.view_name.toString())){
holder.view_gender .setMyEditViewClik(this, R.id.view_gender);
}
147) Handler的靜態(tài)使用
handler = new MyHandler(this);
static class MyHandler extends Handler{
WeakReference<UpdateService> weakRef;
MyHandler(UpdateService updateService){
weakRef = new WeakReference<>(weakRefItem);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
EcgGatherActivityBase activity = activityRef.get();
if(activity == null){
return;
}
switch (msg.arg1){
case SAVEFINISH:
MCToast.show(R.string.common_save_success, activity);
activity.finish();
break;
default:
break;
}
}
}
148) 網(wǎng)絡(luò)連接超時(shí)設(shè)置
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 3000); //設(shè)置連接超時(shí)
HttpConnectionParams.setSoTimeout(httpParameters, 3000); //設(shè)置請(qǐng)求超時(shí)
HttpClient client = new DefaultHttpClient(httpParameters);
HttpGet mothod = new HttpGet(jasonUrl);
HttpResponse httpResponse = client.execute(mothod);
149) List 去除元素的方法
if (subList.indexOf(subScribe) >= 0){
subList.remove(subScribe);
}
150) 流轉(zhuǎn)轉(zhuǎn)化成字符串
public static String inputStream2String(InputStream is) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = -1;
while ((i = is.read()) != -1) {
baos.write(i);
}
return baos.toString();
}