• android平台的图文上下滚动与左右滚动效果


    首先布局文件:

    common_info_item_view.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="fill_parent"
    android:orientation
    ="vertical"
    android:id
    ="@+id/linear"
    >
    <TextView
    android:id="@+id/text_title"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:padding
    ="5dip"
    android:layout_marginTop
    ="5dip"
    android:gravity
    ="center"
    android:textSize
    ="20sp"
    />
    <Com.Takewin.net.FriendlyScrollView
    android:id="@+id/scroll"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="fill_parent"
    android:layout_marginBottom
    ="5dip"
    android:fadeScrollbars
    ="true"
    >
    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height
    ="fill_parent"
    android:orientation
    ="vertical"
    >

    <ImageView
    android:id="@+id/inf_pic"
    android:layout_width
    ="321dp"
    android:layout_height
    ="wrap_content"
    android:maxWidth
    ="600dip"
    android:minHeight
    ="120dip"
    android:src
    ="@drawable/info" />
    <TextView
    android:id="@+id/text_detail"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:lineSpacingMultiplier
    ="1.3"
    android:textSize
    ="18sp"
    android:singleLine
    ="false"
    />
    </LinearLayout>

    </Com.Takewin.net.FriendlyScrollView>
    </LinearLayout>

    common_info_list_view.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id
    ="@+id/common_query_view_layout"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="fill_parent"
    android:orientation
    ="vertical">
    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height
    ="fill_parent"
    android:layout_marginTop
    ="5dip"
    android:orientation
    ="vertical"
    android:gravity
    ="top"
    android:layout_gravity
    ="top">

    <ViewFlipper
    android:id="@+id/viewflipper_data"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="fill_parent"
    android:gravity
    ="top"
    android:layout_gravity
    ="top"
    android:spacing
    ="80dip"
    android:paddingLeft
    ="6dip"
    android:paddingRight
    ="6dip"
    android:layout_marginBottom
    ="60dip"
    >
    </ViewFlipper>
    </LinearLayout>
    </RelativeLayout>

    重写scrollview

    package Com.Takewin.net;

    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.GestureDetector;
    import android.view.MotionEvent;
    import android.widget.ScrollView;

    public class FriendlyScrollView extends ScrollView {

    GestureDetector gestureDetector;

    public FriendlyScrollView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    }

    public FriendlyScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    }

    public FriendlyScrollView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
    }

    public void setGestureDetector(GestureDetector gestureDetector) {
    this.gestureDetector = gestureDetector;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event);
    return gestureDetector.onTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev){
    gestureDetector.onTouchEvent(ev);
    super.dispatchTouchEvent(ev);
    return true;
    }

    }

    主文件:

    /**
    * filename : ShowView.java
    * company : TakeWin Limited (android) 2012-2015
    * compiler : eclipse 3.7.1
    * remark : TODO
    * created : 2012-3-28 weeds
    * changed : 后来者居下
    */

    package Com.Takewin.net;

    import java.io.BufferedReader;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;

    import org.apache.http.util.EncodingUtils;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.GestureDetector;
    import android.view.LayoutInflater;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.Window;
    import android.view.WindowManager;
    import android.view.GestureDetector.SimpleOnGestureListener;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.widget.ViewFlipper;

    public class ShowView extends Activity{
    File outfile;
    TextView tv;
    private ViewFlipper viewFlipper;

    private String[] descriptionsArray;
    private String[] titleArray;

    private int selectedPosition;

    private TextView textViewTitle;
    private TextView textViewContent;
    private FriendlyScrollView scroll;

    private LayoutInflater mInflater;

    private GestureDetector gestureDetector;
    private String bookT;
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.common_info_list_view);
    Intent git=this.getIntent();
    bookT=git.getStringExtra("book_title");
    Toast.makeText(this, bookT, 1).show();

    tv=(TextView)findViewById(R.id.info_content);
    InitUI();
    try {
    tv.setText(getContentFromBook());
    } catch (Exception e) {
    }
    }

    private void InitUI(){

    viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper_data);

    mInflater = LayoutInflater.from(this);

    fillDate();

    viewFlipper.addView(getContentView());
    }

    private void fillDate(){
    selectedPosition = 0;


    try {
    descriptionsArray=new String[]{getContentFromBook(),getContentFromBook(),getContentFromBook(),getContentFromBook()};
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    titleArray = new String[]{"0001","002","003","004"};
    // descriptionsArray = new String[]{"内容0001","内容002","内容003","内容004"};
    if(descriptionsArray.length==0 && descriptionsArray.equals(""))
    {
    descriptionsArray = new String[]{"内容0001","内容002","内容003","内容004"};
    }

    gestureDetector = new GestureDetector(new CommonGestureListener());
    }

    private View getContentView() {
    View contentView = new View(this);
    contentView = mInflater.inflate(R.layout.common_info_item_view, null);

    textViewTitle = (TextView) contentView.findViewById(R.id.text_title);
    textViewContent = (TextView) contentView.findViewById(R.id.text_detail);

    textViewTitle.setText(titleArray[selectedPosition]);
    textViewTitle.setText(bookT);
    textViewTitle.setPadding(10, 0, 10, 0);
    textViewContent.setText(descriptionsArray[selectedPosition]);
    textViewContent.setPadding(10, 5, 10, 5);

    scroll = (FriendlyScrollView) contentView.findViewById(R.id.scroll);
    scroll.setOnTouchListener(onTouchListener);
    scroll.setGestureDetector(gestureDetector);

    return contentView;
    }

    private View.OnTouchListener onTouchListener = new View.OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    return gestureDetector.onTouchEvent(event);
    }
    };
    //此函数是读取SD卡的文本文件的内容
    public String getContentFromBook() throws Exception
    { String re="";
    File path=new File("/sdcard/linebook");
    outfile=new File("/mnt/sdcard/test1.txt");
    if(path.exists()&& outfile.exists())
    {


    FileInputStream istream=new FileInputStream(outfile);
    int num=(int)istream.available();
    byte[] buffer = new byte[num+10];
    istream.read(buffer, 0, num);
    //re=EncodingUtils.getString(buffer,"gb2312");
    re=EncodingUtils.getString(buffer,"gb2312");

    }else{
    path.mkdirs();
    outfile.createNewFile();

    }
    return re.trim();

    }



    public class CommonGestureListener extends SimpleOnGestureListener {

    @Override
    public boolean onDown(MotionEvent e) {
    // TODO Auto-generated method stub
    Log.d("QueryViewFlipper", "====> Jieqi: do onDown...");
    return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
    // TODO Auto-generated method stub
    Log.d("QueryViewFlipper", "====> Jieqi: do onShowPress...");
    super.onShowPress(e);
    }

    @Override
    public void onLongPress(MotionEvent e) {
    // TODO Auto-generated method stub
    Log.d("QueryViewFlipper", "----> Jieqi: do onLongPress...");
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
    // TODO Auto-generated method stub
    Log.d("QueryViewFlipper", "====> Jieqi: do onSingleTapConfirmed...");
    return false;
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
    // TODO Auto-generated method stub
    Log.d("QueryViewFlipper", "====> Jieqi: do onSingleTapUp...");
    return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
    float velocityY){
    // TODO Auto-generated method stub
    Log.d("QueryViewFlipper", "====> Jieqi: do onFling...");
    if (e1.getX() - e2.getX() > 100 && Math.abs(velocityX) > 50) {
    //����
    selectedPosition = selectedPosition + 1 < titleArray.length ? (selectedPosition + 1) : 0;
    viewFlipper.addView(getContentView());
    viewFlipper.setInAnimation(AnimationControl.inFromRightAnimation());
    viewFlipper.setOutAnimation(AnimationControl.outToLeftAnimation());
    viewFlipper.showNext();
    viewFlipper.removeViewAt(0);
    } else if (e2.getX() - e1.getX() > 100 && Math.abs(velocityX) > 50) {
    //����
    selectedPosition = selectedPosition > 0 ? (selectedPosition - 1) : (titleArray.length - 1);
    viewFlipper.addView(getContentView());
    viewFlipper.setInAnimation(AnimationControl.inFromLeftAnimation());
    viewFlipper.setOutAnimation(AnimationControl.outToRightAnimation());
    viewFlipper.showNext();
    viewFlipper.removeViewAt(0);
    }
    return true;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
    float distanceX, float distanceY) {
    // TODO Auto-generated method stub
    Log.d("QueryViewFlipper", "====> Jieqi: do onScroll...");
    return super.onScroll(e1, e2, distanceX, distanceY);
    }

    }
    }

    要是调试,布局文件有些图片自己换咯!

    其实我是想做一个像UC或腾讯qq的那种手拖动效果,可惜之前找了一个demo是可以,但要是文本内容过多就显示不出来...

    找个时间也重写一下ScrollView




     

  • 相关阅读:
    从零开始,开发一个 Web Office 套件(8):状态管理 & 拖动鼠标选中文字
    从零开始,开发一个 Web Office 套件(9):拖动鼠标选中文字 Edge Case
    从零开始, 开发一个 Web Office 套件 (1): 富文本编辑器
    从零开始,开发一个 Web Office 套件(6):光标 & Click 事件
    20211912 202120222 《网络攻防实践》第一周作业
    3天实践亲测简约靠谱Win10linux子系统Ubuntu下显示图形界面搭建固定IP远程服务器
    安卓通知转发
    RocketMQ(8) 消费幂等
    RocketMQ(5) 订阅关系的一致性规范
    RocketMQ(10) 消息类型
  • 原文地址:https://www.cnblogs.com/weeds/p/android.html
Copyright © 2020-2023  润新知