以下代码正确,可以正确运行:
【ImageGallery.java】
import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.animation.Transformation; import android.widget.Gallery; @SuppressWarnings("deprecation") public class ImageGallery extends Gallery { public ImageGallery(Context context, AttributeSet paramAttributeSet) { super(context, paramAttributeSet); } //return false 一次滑动一张 @Override public boolean onFling(MotionEvent paramMotionEvent1, MotionEvent paramMotionEvent2, float paramFloat1, float paramFloat2) { return false; } @Override public boolean onScroll(MotionEvent paramMotionEvent1, MotionEvent paramMotionEvent2, float paramFloat1, float paramFloat2) { float f = 1.4F * paramFloat1; return super.onScroll(paramMotionEvent1, paramMotionEvent2, f, paramFloat2); } @Override protected boolean getChildStaticTransformation(View child, Transformation t) { return true; } }
import com.como.adapter.ImageAdapter; import com.como.entity.ConstantS; import com.sina.weibo.sdk.WeiboSDK; import com.sina.weibo.sdk.api.BaseResponse; import com.sina.weibo.sdk.api.IWeiboAPI; import com.sina.weibo.sdk.api.IWeiboHandler; import com.sina.weibo.sdk.api.ImageObject; import com.sina.weibo.sdk.api.SendMessageToWeiboRequest; import com.sina.weibo.sdk.api.SendMultiMessageToWeiboRequest; import com.sina.weibo.sdk.api.TextObject; import com.sina.weibo.sdk.api.WeiboMessage; import com.sina.weibo.sdk.api.WeiboMultiMessage; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.view.View; import android.view.Window; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Gallery; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; @SuppressWarnings("deprecation") public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener, IWeiboHandler.Response { /** 微博OpenAPI访问入口 */ IWeiboAPI mWeiboAPI = null; private Gallery myGallery; private ImageView iBack, iShare, iCollect, iAtt, iRemark; private TextView tContent; public Integer[] mImageIds = { R.drawable.p1_choose, R.drawable.p2_choose, R.drawable.p3_choose, R.drawable.p4_choose, R.drawable.p5_choose, R.drawable.p6_choose }; private Boolean flag = true; private ImageAdapter adapter; Bitmap bit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); iBack = (ImageView) findViewById(R.id.imageView2); iShare = (ImageView) findViewById(R.id.imageView5); iCollect = (ImageView) findViewById(R.id.imageView4); iAtt = (ImageView) findViewById(R.id.imageView3); iRemark = (ImageView) findViewById(R.id.editText1); tContent = (TextView) findViewById(R.id.textView1); myGallery = (Gallery) findViewById(R.id.myGallery); adapter = new ImageAdapter(this, mImageIds); myGallery.setHorizontalFadingEdgeEnabled(false); myGallery.setAdapter(adapter); myGallery.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { if (flag) { iBack.setVisibility(View.GONE); iShare.setVisibility(View.GONE); iCollect.setVisibility(View.GONE); iAtt.setVisibility(View.GONE); iRemark.setVisibility(View.GONE); tContent.setVisibility(View.GONE); flag = false; } else { iBack.setVisibility(View.VISIBLE); iShare.setVisibility(View.VISIBLE); iCollect.setVisibility(View.VISIBLE); iAtt.setVisibility(View.VISIBLE); iRemark.setVisibility(View.VISIBLE); tContent.setVisibility(View.VISIBLE); flag = true; } } }); myGallery.setOnItemSelectedListener(this); iShare.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mWeiboAPI.registerApp(); reqMsg(true, true, false, false, false, false); } }); // 创建微博对外接口实例 mWeiboAPI = WeiboSDK.createWeiboAPI(this, ConstantS.APP_KEY); mWeiboAPI.responseListener(getIntent(), this); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); mWeiboAPI.responseListener(intent, this); } /** * 从本应用->微博->本应用 接收响应数据,该方法被调用。 注意:确保{@link #onCreate(Bundle)} 与 * {@link #onNewIntent(Intent)}中, 调用 mWeiboAPI.responseListener(intent, * this) */ @Override public void onResponse(BaseResponse baseResp) { switch (baseResp.errCode) { case com.sina.weibo.sdk.constant.Constants.ErrorCode.ERR_OK: Toast.makeText(this, "成功!!", Toast.LENGTH_LONG).show(); break; case com.sina.weibo.sdk.constant.Constants.ErrorCode.ERR_CANCEL: Toast.makeText(this, "用户取消!!", Toast.LENGTH_LONG).show(); break; case com.sina.weibo.sdk.constant.Constants.ErrorCode.ERR_FAIL: Toast.makeText(this, baseResp.errMsg + ":失败!!", Toast.LENGTH_LONG) .show(); break; } } public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { switch (position) { case 0: tContent.setText("大连市人民广场的草地。时任市长的薄熙来当年在各大广场种上绿草。这种比植树更快的城市快速绿化手法让大连闻名全国。这种国外引进、价格不菲的绿草也被人称为“熙来草”。"); break; case 1: tContent.setText("大连市人民广场,1993年以前该广场名为“斯大林广场”。位于市政府南面,占地12万平方米。草坪多、广场多,是薄熙来主政大连期间城市建设的惯用手法,原有的广场和草坪也在期间得到扩张翻修。"); break; case 2: tContent.setText("“水晶球”,当地人戏称“薄熙球”,位于大连市友好广场,直径为15米,表面由3120块玻璃围合而成,下方设计成黄、黑、红、白、棕五种颜色的巨手托起球体。构思设计来自于时任大连市长。"); break; case 3: tContent.setText("“大足球”,这个曾无数次出现在各种城市宣传照片中的“大足球”是大连的地标之一,也就是薄熙来在庭审时最后陈述中提到的他设计的“足球博物馆”。"); break; case 4: tContent.setText("大连市星海广场,号称亚洲最大的城市广场,大连市主要观光景点之一。建成于1997年,原为星海湾的一个废弃盐场。薄熙来在大连主政期间最有代表性的政绩工程之一。"); break; case 5: tContent.setText("简易的灵棚里挂起了8张遗像。抱着来悼念的表姐,孙晓霞的小姑子李玉清忍不住失声痛哭。原来房子的位置如今成了8位家人的灵堂。"); break; default: break; } bit = adapter.getImgBitmap(position); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } private void reqMsg(boolean hasText, boolean hasImage, boolean hasWebpage, boolean hasMusic, boolean hasVedio, boolean hasVoice) { if (mWeiboAPI.isWeiboAppSupportAPI()) { Toast.makeText(this, "当前微博版本支持SDK分享", Toast.LENGTH_SHORT).show(); int supportApi = mWeiboAPI.getWeiboAppSupportAPI(); if (supportApi >= 10351) { Toast.makeText(this, "当前微博版本支持多条消息,Voice消息分享", Toast.LENGTH_SHORT).show(); reqMultiMsg(hasText, hasImage, hasWebpage, hasMusic, hasVedio, hasVoice); } else { Toast.makeText(this, "当前微博版本只支持单条消息分享", Toast.LENGTH_SHORT) .show(); reqSingleMsg(hasText, hasImage, hasWebpage, hasMusic, hasVedio/* * , * hasVoice */); } } else { Toast.makeText(this, "当前微博版本不支持SDK分享", Toast.LENGTH_SHORT).show(); } } /** * 第三方应用发送请求消息到微博,唤起微博分享界面。 注意:当isWeiboAppSupportAPI() >= 10351 * 时,支持同时分享多条消息, 同时可以分享文本、图片以及其它媒体资源(网页、音乐、视频、声音中的一种), 并且支持Voice消息。 * * @param hasText * 分享的内容是否有文本 * @param hasImage * 分享的内容是否有图片 * @param hasWebpage * 分享的内容是否有网页 * @param hasMusic * 分享的内容是否有音乐 * @param hasVideo * 分享的内容是否有视频 * @param hasVoice * 分享的内容是否有声音 */ private void reqMultiMsg(boolean hasText, boolean hasImage, boolean hasWebpage, boolean hasMusic, boolean hasVideo, boolean hasVoice) { // 1. 初始化微博的分享消息 WeiboMultiMessage weiboMessage = new WeiboMultiMessage(); if (hasText) { weiboMessage.textObject = getTextObj(); } if (hasImage) { weiboMessage.imageObject = getImageObj(); } // 2. 初始化从第三方到微博的消息请求 SendMultiMessageToWeiboRequest req = new SendMultiMessageToWeiboRequest(); // 用transaction唯一标识一个请求 req.transaction = String.valueOf(System.currentTimeMillis()); req.multiMessage = weiboMessage; // 3. 发送请求消息到微博,唤起微博分享界面 mWeiboAPI.sendRequest(this, req); } /** * 第三方应用发送请求消息到微博,唤起微博分享界面。 当isWeiboAppSupportAPI() < 10351 只支持分享单条消息,即 * 文本、图片、网页、音乐、视频中的一种,不支持Voice消息。 * * @param hasText * 分享的内容是否有文本 * @param hasImage * 分享的内容是否有图片 * @param hasWebpage * 分享的内容是否有网页 * @param hasMusic * 分享的内容是否有音乐 * @param hasVideo * 分享的内容是否有视频 */ private void reqSingleMsg(boolean hasText, boolean hasImage, boolean hasWebpage, boolean hasMusic, boolean hasVideo/* * , boolean * hasVoice */) { // 1. 初始化微博的分享消息 // 用户可以分享文本、图片、网页、音乐、视频中的一种 WeiboMessage weiboMessage = new WeiboMessage(); if (hasText) { weiboMessage.mediaObject = getTextObj(); } if (hasImage) { weiboMessage.mediaObject = getImageObj(); } // 2. 初始化从第三方到微博的消息请求 SendMessageToWeiboRequest req = new SendMessageToWeiboRequest(); // 用transaction唯一标识一个请求 req.transaction = String.valueOf(System.currentTimeMillis()); req.message = weiboMessage; // 3. 发送请求消息到微博,唤起微博分享界面 mWeiboAPI.sendRequest(this, req); } /** * 文本消息构造方法。 * * @return 文本消息对象。 */ private TextObject getTextObj() { TextObject textObject = new TextObject(); textObject.text = tContent.getText().toString(); return textObject; } /** * 图片消息构造方法。 * * @return 图片消息对象。 */ private ImageObject getImageObj() { ImageObject imageObject = new ImageObject(); imageObject.setImageObject(bit); return imageObject; } }
import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; @SuppressWarnings("deprecation") public class ImageAdapter extends BaseAdapter { // 用来设置ImageView的风格 int mGalleryItemBackground; private Context context; private Integer[] mImageIds; // 构造函数 public ImageAdapter(Context context, Integer[] mImageIds) { // TODO Auto-generated constructor stub this.context = context; this.mImageIds = mImageIds; } public Bitmap getImgBitmap(int position) { Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), mImageIds[position]); return bitmap; } // 返回所有图片的个数 @Override public int getCount() { // TODO Auto-generated method stub return mImageIds.length; } // 返回图片在资源的位置 @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } // 返回图片在资源的位置 @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } // 此方法是最主要的,他设置好的ImageView对象返回给Gallery @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ImageView imageView = new ImageView(context); // 通过索引获得图片并设置给ImageView imageView.setImageResource(mImageIds[position]); // 设置ImageView的伸缩规格,用了自带的属性值 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); // 设置布局参数 imageView.setLayoutParams(new Gallery.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); return imageView; } public int getmGalleryItemBackground() { return mGalleryItemBackground; } public void setmGalleryItemBackground(int mGalleryItemBackground) { this.mGalleryItemBackground = mGalleryItemBackground; } }