首页 文章 精选 留言 我的

精选列表

搜索[UI设计],共10000篇文章
优秀的个人博客,低调大师

Android UI学习 - GridView和ImageView的使用

GridView: A view that shows items in two-dimensional scrolling grid. The items in the grid come from the ListAdapterassociated with this view. 简单说GridView就是我们资源管理器平常见到的一个个文件的icon显示方式。 上面提及到了GridView的Item是来自ListAdapter的所以一般在Activity的onCreate使用GridView的代码 @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.grid_2); GridViewg=(GridView)findViewById(R.id.myGrid); g.setAdapter(newImageAdapter(this)); } 而ImageAdapter一般是extends BaseAdapter。BaseAdapter是implements ListAdapter SpinnerAdapter但很多时候自定义的Adapter都是override ListAdapter的父类Adapter接口里面的方法 int getCount() 获取当前Adapter的Items数目 Object getItem(int position) 获取相应position的Item long getItemId(int position) 获取相应position的Item在List中的row id View getView(int position, View convertView, ViewGroup parent) 获取在指定position所要显示的data的View 这些方法函数和swing的差不多都是基于MVC。大概原理过程是这样的程序需要显示GridView那么要把data一个一个地显示出来是通过一个for循环首先call Adapter.getCount()得到有多少个data然后position++地getItemgetView得到要显示的view,这样子逐一地显示出来 下面是官方sample里面的Photo Grid的例子本人省略了某些代码 publicclassImageAdapterextendsBaseAdapter{ publicImageAdapter(Contextc){ mContext=c; } publicintgetCount(){ returnmThumbIds.length; } publicObjectgetItem(intposition){ returnposition; } publiclonggetItemId(intposition){ returnposition; } publicViewgetView(intposition,ViewconvertView,ViewGroupparent){ ImageViewimageView; if(convertView==null){ imageView=newImageView(mContext); imageView.setLayoutParams(newGridView.LayoutParams(45,45));//设置ImageView宽高 imageView.setAdjustViewBounds(false); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8,8,8,8); }else{ imageView=(ImageView)convertView; } imageView.setImageResource(mThumbIds[position]); returnimageView; } privateContextmContext; privateInteger[]mThumbIds={ R.drawable.sample_thumb_0,R.drawable.sample_thumb_1, R.drawable.sample_thumb_2,R.drawable.sample_thumb_3, R.drawable.sample_thumb_4,R.drawable.sample_thumb_5, R.drawable.sample_thumb_6,R.drawable.sample_thumb_7 }; } 留意getView里面的代码要判断convertView是否为null以便重用减少对象的创建减少内存占用。 XML布局文件内容原来就只是指明GridView <?xmlversion="1.0"encoding="utf-8"?> <GridViewxmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myGrid" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:numColumns="auto_fit" android:columnWidth="60dp" android:stretchMode="columnWidth" android:gravity="center" /> 可以看到getView和ImageView是重点影响图片的显示效果。而且发现列数是不确定的取决于每个ImageView的宽度和屏幕的宽度。接下来看看ImageView。 ImageViewDisplays an arbitrary image, such as an icon. The ImageView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the image so that it can be used in any layout manager, and provides various display options such as scaling and tinting。 ImageView就是用来显示Imageicon的。 这里我们重点理解ImageView的属性android:scaleType即ImageView.setScaleType(ImageView.ScaleType)。android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType /android:scaleType值的意义区别 CENTER /center按图片的原来size居中显示当图片长/宽超过View的长/宽则截取图片的居中部分显示 CENTER_CROP /centerCrop按比例扩大图片的size居中显示使得图片长(宽)等于或大于View的长(宽) CENTER_INSIDE /centerInside将图片的内容完整居中显示通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽 FIT_CENTER / fitCenter把图片按比例扩大/缩小到View的宽度居中显示 FIT_END /fitEnd 把图片按比例扩大/缩小到View的宽度显示在View的下部分位置 FIT_START /fitStart 把图片按比例扩大/缩小到View的宽度显示在View的上部分位置 FIT_XY /fitXY把图片 不按比例扩大/缩小到View的大小显示 MATRIX / matrix用矩阵来绘制 一开始我不明白MATRIX矩阵网上搜索后发现原来MATRIX矩阵可以动态缩小放大图片来显示这里不展开深入的了解只是贴出相关语句缩小图片 //获得Bitmap的高和宽 intbmpWidth=bmp.getWidth(); intbmpHeight=bmp.getHeight(); //设置缩小比例 doublescale=0.8; //计算出这次要缩小的比例 scaleWidth=(float)(scaleWidth*scale); scaleHeight=(float)(scaleHeight*scale); //产生resize后的Bitmap对象 Matrixmatrix=newMatrix(); matrix.postScale(scaleWidth,scaleHeight); BitmapresizeBmp=Bitmap.createBitmap(bmp,0,0,bmpWidth,bmpHeight,matrix,true); 应用ImageView的例子很多看看上次FrameLayout里面的 <ImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="center" android:src="@drawable/candle" /> ** 要注意一点我发现Drawable文件夹里面的图片命名是不能大写的。 本文转自 Icansoft 51CTO博客原文链接http://blog.51cto.com/android/316255

优秀的个人博客,低调大师

Android:UI控件风格与主题、selector、Theme

注:默认设置放在最后。 1.颜色xml:values-->resources-->color.xml 1 2 3 4 5 6 <?xmlversion= "1.0" encoding= "utf-8" ?> <resources> <colorname= "yellow" >#FFFF00</color> <colorname= "zise" >#D15FEE</color> <colorname= "lightblue" >#8DB6CD</color> </resources> 2.颜色selector:Color List-->selector-->/color/xxx.xml 1 2 3 4 5 6 <?xmlversion= "1.0" encoding= "utf-8" ?> <selectorxmlns:android= "http://schemas.android.com/apk/res/android" > <itemandroid:state_pressed= "true" android:color= "@color/yellow" /> <itemandroid:color= "@color/zise" /> </selector> 3.尺寸(文件大小、控件大小等)dimens:values-->dimens.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <resources> <!--Defaultscreenmargins,pertheAndroidDesignguidelines.--> <dimenname= "activity_horizontal_margin" >16dp</dimen> <dimenname= "activity_vertical_margin" >16dp</dimen> <dimenname= "btn_head_width" >100dp</dimen> <dimenname= "btn_head_high" >100dp</dimen> <dimenname= "edit_width" >200dp</dimen> <dimenname= "edit_high" >60dp</dimen> <dimenname= "edit_width_xhdpi" >60dp</dimen> <dimenname= "edit_high_xhdpi" >160dp</dimen> <dimenname= "textsize" >20dp</dimen> <dimenname= "EditText_layout_marginTop" >80dp</dimen> <dimenname= "textView_layout_marginTop" >26dp</dimen> </resources> 4.背景图片:Drawable-->selector-->/drawable/xxx.xml 1 2 3 4 5 6 <?xmlversion= "1.0" encoding= "utf-8" ?> <selectorxmlns:android= "http://schemas.android.com/apk/res/android" > <itemandroid:state_pressed= "true" android:drawable= "@drawable/ic_pressed" ></item> <itemandroid:drawable= "@drawable/ic_default" ></item> </selector> 5.主题相关 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 常用的系统主题: 1 .没有标题: @android :style/Theme.Black.NoTitleBar 2 .全屏幕 @android :style/Theme.Black.NoTitleBar.Fullscreen 3 .透明 @android :style/Theme.Translucent 4 .桌面壁纸作为背景 @android :style/Theme.Wallpaper 5 .对话框 @android :style/Theme.Dialog 对Activity的生命周期是否有影响? 练习:第一个Activity中有两个按钮, 一个弹出对话框主题的Activity, 另一个弹出对话框,测试二者对 第一个Activity生命周期是否有影响。 弹出对话框主题的Activity: 暂停状态,onPause() 弹出对话框 生命周期没有影响 使用场景: 1 .对话框主题的Activity 例如:在桌面弹出短信内容对话框 对话框后面的Activity是其他App的,这时必须使用对话框主题的 Activity 2 .对话框 需要使用Activity中的成员变量时 6.设置一按钮点击时图片和文字颜色都有高亮效果 1 2 3 4 5 6 7 8 9 10 11 12 13 <TextView android:id= "@+id/home_tv_city" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_alignParentLeft= "true" android:layout_centerVertical= "true" android:drawableLeft= "@drawable/selector_home_citymore" android:drawablePadding= "4dp" android:gravity= "center" android:paddingRight= "12dp" android:text= "厦门" android:textColor= "@color/selector_home_city" android:textSize= "20sp" /> res/drawable/selector_home_citymore.xml代码: 1 2 3 4 5 6 <?xmlversion= "1.0" encoding= "utf-8" ?> <selectorxmlns:android= "http://schemas.android.com/apk/res/android" > <itemandroid:state_pressed= "true" android:drawable= "@drawable/home_morecity_pre" ></item> <itemandroid:drawable= "@drawable/home_morecity_nor" ></item> </selector> res/color/selector_home_city.xml代码: 1 2 3 4 5 6 <?xmlversion= "1.0" encoding= "utf-8" ?> <selectorxmlns:android= "http://schemas.android.com/apk/res/android" > <itemandroid:state_pressed= "true" android:color= "@color/cyan" /> <itemandroid:color= "@color/white" /> </selector> 7.创建一个shape的selector 1 2 3 4 5 6 7 8 9 10 11 <?xmlversion= "1.0" encoding= "utf-8" ?> <selectorxmlns:android= "http://schemas.android.com/apk/res/android" > <itemandroid:state_pressed= "true" ><shape> <solidandroid:color= "@color/gray_text" /> </shape></item> <item><shape> <solidandroid:color= "@color/touming" /> </shape></item> </selector> 8.代码编写selector 范例: 布局xml代码: 1 2 3 4 5 6 7 8 <TextView android:id= "@+id/TextView_title" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:focusable= "true" android:drawableTop= "@drawable/selector_tabwidget_icon" android:textAlignment= "center" /> selector的xml代码: 1 2 3 4 5 6 7 8 9 10 11 12 <?xmlversion= "1.0" encoding= "utf-8" ?> <selectorxmlns:android= "http://schemas.android.com/apk/res/android" > <!--Nonfocusedstates--> <itemandroid:state_focused= "false" android:state_selected= "false" android:state_pressed= "false" android:drawable= "@drawable/contact" /> <itemandroid:state_focused= "false" android:state_selected= "true" android:state_pressed= "false" android:drawable= "@drawable/contact_sel" /> <!--Focusedstates--> <itemandroid:state_focused= "true" android:state_selected= "false" android:state_pressed= "false" android:drawable= "@drawable/contact_sel" /> <itemandroid:state_focused= "true" android:state_selected= "true" android:state_pressed= "false" android:drawable= "@drawable/contact_sel" /> <!--Pressed--> <itemandroid:state_selected= "true" android:state_pressed= "true" android:drawable= "@drawable/contact_sel" /> <itemandroid:state_pressed= "true" android:drawable= "@drawable/contact_sel" /> </selector> selector的java代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 StateListDrawabledrawable= new StateListDrawable(); //Nonfocusedstates drawable.addState( new int []{-android.R.attr.state_focused,-android.R.attr.state_selected,-android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.contact)); drawable.addState( new int []{-android.R.attr.state_focused,android.R.attr.state_selected,-android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.contact_sel)); //Focusedstates drawable.addState( new int []{android.R.attr.state_focused,-android.R.attr.state_selected,-android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.contact_sel)); drawable.addState( new int []{android.R.attr.state_focused,android.R.attr.state_selected,-android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.contact_sel)); //Pressed drawable.addState( new int []{android.R.attr.state_selected,android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.contact_sel)); drawable.addState( new int []{android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.contact_sel)); TextViewtextView=(TextView)findViewById(R.id.TextView_title); textView.setCompoundDrawablesWithIntrinsicBounds( null ,drawable, null , null ); 本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1208480,如需转载请自行联系原作者

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册