Android 总结三
来源:互联网 发布:淘宝买的壁纸怎么贴 编辑:程序博客网 时间:2024/06/07 15:22
1、打开raw资源文件
AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.beep);
2、打开assert里的文件
InputStream inputStream = getAssets().open("tangyan.jpg");
3、mediaPlayer的初始化
// 设置声道流格式为音乐setVolumeControlStream(AudioManager.STREAM_MUSIC);MediaPlayer mediaPlayer = new MediaPlayer();mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//设置声音完成后监听mediaPlayer.setOnCompletionListener(beepListener);AssetFileDescriptor file = getResources().openRawResourceFd( R.raw.beep);try { mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength()); file.close(); mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);//设置音量 mediaPlayer.prepare();} catch (IOException e) { mediaPlayer = null;}/** * 当声音播放完成后,播放点回到原点 */private final MediaPlayer.OnCompletionListener beepListener = new MediaPlayer.OnCompletionListener() { public void onCompletion(MediaPlayer mediaPlayer) { mediaPlayer.seekTo(0); }};
4、activity动画切换
overridePendingTransition(R.anim.activity_in_from_rigth, R.anim.activity_out_to_scale);
5、压缩图片
ByteArrayOutputStream baos = new ByteArrayOutputStream();bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);byte[] bitmapByte = baos.toByteArray();intent.putExtra("bitmap", bitmapByte);
6、显示图片的一段区域
ImageView mImageView = (ImageView) findViewById(R.id.id_imageview);try { InputStream inputStream = getAssets().open("tangyan.jpg"); //获得图片的宽、高 BitmapFactory.Options tmpOptions = new BitmapFactory.Options(); tmpOptions.inJustDecodeBounds = true; BitmapFactory.decodeStream(inputStream, null, tmpOptions); int width = tmpOptions.outWidth; int height = tmpOptions.outHeight; //设置显示图片的中心区域 BitmapRegionDecoder bitmapRegionDecoder = BitmapRegionDecoder.newInstance(inputStream, false); BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.RGB_565; Bitmap bitmap = bitmapRegionDecoder.decodeRegion(new Rect(width / 2 - 100, height / 2 - 100, width / 2 + 100, height / 2 + 100), options); mImageView.setImageBitmap(bitmap);} catch (IOException e){ e.printStackTrace();}
7、onTouchEvent里的
getX与getY指的是以当前前控件的左上角所在的坐标为(0,0)原点getX > 0 and getX< getWidthgetY > 0 and getY< getHeight
8、图片缩放的时候必须invalidate();
9、把事件交给手势处理
@Overridepublic boolean onTouch(View v, MotionEvent event) { if (v == mOriginalImageView) { mGestureDetector.onTouchEvent(event); return true; } return false;}
10、listview的内容变化了,一定要用notifyDataSetChanged通知
protected void expandOrCollapse(int position) { Node node = mNodes.get(position); if (node!= null) { if (!node.isLeaf()) { //表示如果关闭的就打开,如果打开的就关闭 node.setExpend(!node.isExpend()); //重新赋值 mNodes = TreeHelper.filterVisibleNode(mAllNodes); //通知视图改变了 notifyDataSetChanged(); } }}
11、读取属性文件
TypedArray a = obtainStyledAttributes(R.styleable.NamecardImageGalleryStyle);galleryItemBackground = a.getResourceId(R.styleable.NamecardImageGalleryStyle_android_galleryItemBackground, 0);a.recycle();
12、过滤jpg文件
/** * 过滤jpg文件 */private class JpgFileFilter implements FilenameFilter { @Override public boolean accept(File dir, String filename) { filename = filename.toLowerCase(); return filename.endsWith(".jpg") || filename.endsWith(".jpeg") ; }}public class ImageFileFilter implements FileFilter{ @Override public boolean accept(File pathname) { String filename = pathname.getName().toLowerCase(); return filename.endsWith(".jpg")||filename.endsWith(".jpeg"); } }
13、得到所有文件
/** * 得到所有文件 * @param path * @return */public static ArrayList<File> getAllFiles(File path) { ArrayList<File> list = new ArrayList<File>(); // 递归取得目录下的所有文件及文件夹 File[] files = path.listFiles(); for (int i = 0; i < files.length; i++) { File file = files[i]; list.add(file); if (file.isDirectory()) { getAllFiles(file); } } return list;}
14、数组合并
/** * 数组合并 * * @param strs */public static <T> Object[] addArrays(T[]... strs) { List<T> list = new ArrayList<T>(Arrays.asList(strs[0])); for (int i = 1; i < strs.length; i++) { list.addAll(Arrays.asList(strs[i])); } Object[] c = list.toArray(); return c;}
15、读取uri所在的图片
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri); 16、获得图片的宽高 Rect rect = getDrawable().getBounds(); Rect rect = getDrawable().getBounds(); int imageHeight = rect.bottom - rect.top; int imageWidth = rect.right - rect.left;
17、listview移动到位置
//有动画移动 mListView.smoothScrollToPosition(mLastPoint); //直接定位 listView.setSelection(lastPoint); 18、文件拷贝 // 对List进行排序Comparator<File> comparator = new FileComparator();Collections.sort(tempFolder, comparator);Collections.sort(tempFile, comparator);// 重新整理datas数组中的数据mFiles = new File[tempFolder.size() + tempFile.size()];// 把临时文件夹中的内容添加到mFiles集合中System.arraycopy(tempFolder.toArray(), 0, mFiles, 0, tempFolder.size());// 把临时文件中的内容添加到mFiles集合中,大小是tempFolder.size()增加System.arraycopy(tempFile.toArray(), 0, mFiles, tempFolder.size(), tempFile.size());
19、fragment添加 tabhost
private void addTabFragment() { ArrayList<TabMode> listTabModes = new ArrayList<TabMode>(); TabMode tabMode = new TabMode(TAB_HOME, R.drawable.tab_1_selector, "首页", R.color.tab_text_color_selector, new TabHomeFragment(), mCurrentTab == TAB_HOME); listTabModes.add(tabMode); tabMode = new TabMode(TAB_SEARCH, R.drawable.tab_2_selector, "好友", R.color.tab_text_color_selector, new TabFriendFragment(), mCurrentTab == TAB_SEARCH); listTabModes.add(tabMode); tabMode = new TabMode(TAB_INFO, R.drawable.tab_3_selector, "通讯录", R.color.tab_text_color_selector, new InfomationFragment(), mCurrentTab == TAB_INFO); listTabModes.add(tabMode); tabMode = new TabMode(TAB_MORE, R.drawable.tab_4_selector, "更多", R.color.tab_text_color_selector, new TabMoreFragment(), mCurrentTab == TAB_MORE); listTabModes.add(tabMode); mTabFragment.creatTab(MainActivity.this, listTabModes, new TabFragment.IFocusChangeListener() { @Override public void OnFocusChange(int currentTabId, int tabIndex) { } });}
20。退出所有的fragment
int backStackCount = getFragmentManager().getBackStackEntryCount();for (int i = 0; i < backStackCount; i++) { //弹出堆栈中的一个并且显示,也就是代码模拟按下返回键的操作。 getFragmentManager().popBackStack();}
21、常用自定义属性写法
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ColorTrackView); mText = ta.getString(R.styleable.ColorTrackView_text); mTextSize = ta.getDimensionPixelSize( R.styleable.ColorTrackView_text_size, mTextSize); mTextOriginColor = ta.getColor( R.styleable.ColorTrackView_text_origin_color, mTextOriginColor); mTextChangeColor = ta.getColor( R.styleable.ColorTrackView_text_change_color, mTextChangeColor); mProgress = ta.getFloat(R.styleable.ColorTrackView_progress, 0); mDirection = ta.getInt(R.styleable.ColorTrackView_direction, mDirection); ta.recycle(); <?xml version="1.0" encoding="utf-8"?> <resources> <attr name="text" format="string" /> <attr name="text_size" format="dimension" /> <attr name="text_origin_color" format="color|reference" /> <attr name="text_change_color" format="color|reference" /> <attr name="progress" format="float" /> <attr name="direction"> <enum name="left" value="0" /> <enum name="right" value="1" /> </attr> <declare-styleable name="ColorTrackView"> <attr name="text" /> <attr name="text_size" /> <attr name="text_origin_color" /> <attr name="text_change_color" /> <attr name="progress" /> <attr name="direction" /> </declare-styleable> </resources>
0 0
- android sensors 总结(三)
- Android总结(三)
- Android 总结三
- Android 单元测试(三)总结
- Android 面试题总结(三)
- Android常见问题总结(三)
- Android三方分享总结
- Android基础总结(三)
- Android基础总结三:BroadcastReceiver总结一
- Android布局总结三:include总结
- Android面试笔记总结(三)
- Android学习总结三:Context的使用
- 最近总结的android疑惑(三)
- xml布局内容总结(三)--Android
- Android面试题总结(三)
- java android面试题分析总结《三
- Android 基础总结:(三)Activity
- Android富文本编辑器总结(三)
- Android 自定义属性写法
- 【SpringMVC学习07】SpringMVC中的统一异常处理
- VR开发的24个技巧
- 移动开发--优秀博客收集(持续更新)
- Qt中的QAction
- Android 总结三
- 习以为常的键盘——不简单
- Jsoup解析HTML页面数据(又扒到1万+的数据)
- 预编译文件创建和配置
- 微信判断请求是否来自公众账号用户名
- Windows下编译x264
- 2016年6月7日--6月21日(共2小时,剩2955小时)
- 大神blog
- Android studio鼠标提示功能