实现简单的选择头像的小程序
来源:互联网 发布:复旦大数据学院怎么样 编辑:程序博客网 时间:2024/05/17 23:04
package com.example.dialog1;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.view.WindowManager.LayoutParams;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageButton;import android.widget.ImageView;public class MainActivity extends Activity {private Gallery gallery;private ImageButton imageButton;private int[] mImageIds = { 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, };int index = 1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imageButton = (ImageButton) findViewById(R.id.imageButton1);imageButton.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubAlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);alertDialog.setTitle("请选择头像:");LayoutInflater layoutInflater = getLayoutInflater();View view1 = layoutInflater.inflate(R.layout.xuanze, null);alertDialog.setView(view1);gallery = (Gallery) view1.findViewById(R.id.gallery1);gallery.setAdapter(new ImageAdapter());gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {// TODO Auto-generated method stubindex = arg2;}});alertDialog.setPositiveButton("确定", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub//removeDialog(index);imageButton.setImageResource(mImageIds[index]);}});alertDialog.setNegativeButton("取消", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubremoveDialog(index);}});alertDialog.create().show();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}private class ImageAdapter extends BaseAdapter {@Overridepublic int getCount() {// TODO Auto-generated method stubreturn mImageIds.length;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubImageView imageView = new ImageView(MainActivity.this);imageView.setImageResource(mImageIds[position]);imageView.setAdjustViewBounds(true);// 设置固定大小imageView.setMaxHeight(80);imageView.setMaxWidth(120);imageView.setLayoutParams(new Gallery.LayoutParams(50,50));imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);// imageView.setBackgroundResource(mGalleryItemBackground);return imageView;}}}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Gallery android:id="@+id/gallery1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:spacing="10dp" android:background="?android:galleryItemBackground" android:gravity="center_vertical" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" /> <ImageSwitcher android:id="@+id/imageSwitcher1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/gallery1" android:layout_alignParentTop="true" android:layout_marginTop="84dp" > </ImageSwitcher></RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ImageButton android:id="@+id/imageButton1" android:onClick="popDialog" android:layout_width="60dp" android:layout_height="60dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:src="@drawable/sample_thumb_0" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/imageButton1" android:layout_marginLeft="41dp" android:layout_toRightOf="@+id/imageButton1" android:ems="10" android:inputType="text" > <requestFocus /> </EditText></RelativeLayout>
- 实现简单的选择头像的小程序
- 运用ImageButton编写选择头像小程序
- Objective C 实现简单的小程序
- 利用PopupWindow和RecyclerView来实现头像的选择(在电视上选择头像)
- QT实现的类似QQ的头像选择窗
- 简单的头像上传
- 简单的圆形头像
- 使用JavaScript实现选择头像和表情的功能
- 实现头像的上传
- java语言实现月食过程的简单小程序
- 利用socket实现简单的文件传输小程序
- GO语言实现的简单“木马”小程序
- Java 实现的简单图片缩放小程序
- MATLAB实现一个简单的车牌识别小程序
- JAVA spring+jcrop.js实现简单的头像剪裁
- 简单的选择排序实现
- 简单的压缩小程序
- 简单的解压小程序
- 处理swap分区使用过多的操作
- 关于UIToolBar背景图片设置问题
- Android animation 定义动画
- MFC使用第三方CSpreadSheet .
- cookie 跟 Request.IsAuthenticated 之间是什么关系
- 实现简单的选择头像的小程序
- DynamicInsert和DynamicUpdate的用法
- xcode 调试 之终 端打印内存释放错误信息 NSZombieEnabled
- CRF汇总
- windows sdk编程系列文章 --- 直接从硬盘扇区读取文件内容
- Testin发布手机QQ4.1深度评测
- 采访 Lua 发明人的一篇文章
- Android Service 服务(一)—— Service
- PBRT Ubuntu Compile and use