Imageswitcher 中的 Ontouch 多点触控缩放?
我正在尝试在图像切换器上启用捏缩放。我现在陷入如何在 ontouch 方法中声明 imageview 的困境。我意识到 ImageView i = (Imageview) v 是不正确的,我只是不知道将其切换到什么以在 ontouch 中声明 imageswitcher imageview ... 这是我的java和xml。感谢您的帮助!
public class ImageSwitch1 extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory, OnTouchListener {
Matrix matrix = new Matrix();
Matrix eventMatrix = new Matrix();
final static int NONE = 0;
final static int DRAG = 1;
final static int ZOOM = 2;
int touchState = NONE;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.imageswitcher);
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView parent, View v, int position, long id) {
final TextView tv = (TextView)findViewById(R.id.SwitcherText);
mSwitcher.setImageResource(mImageIds[position]);
tv.setText(mText[position]);
}
public void onNothingSelected(AdapterView parent) {
}
public View makeView() {
ImageView i = new ImageView(this);
i.setScaleType(ScaleType.MATRIX);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
i.setOnTouchListener(this);
return i;
}
final static float MIN_DIST = 50;
static float eventDistance = 0;
static float centerX =0, centerY = 0;
public boolean onTouch(View v, MotionEvent event) {
ImageView i = (ImageView) v;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
//primary touch event starts: remember touch down location
touchState = DRAG;
centerX = event.getX(0);
centerY = event.getY(0);
eventMatrix.set(matrix);
break;
case MotionEvent.ACTION_POINTER_DOWN:
//secondary touch event starts: remember distance and center
eventDistance = calcDistance(event);
calcMidpoint(centerX, centerY, event);
if (eventDistance > MIN_DIST) {
eventMatrix.set(matrix);
touchState = ZOOM;
}
break;
case MotionEvent.ACTION_MOVE:
if (touchState == DRAG) {
//single finger drag, translate accordingly
matrix.set(eventMatrix);
matrix.setTranslate(event.getX(0) - centerX,
event.getY(0) - centerY);
} else if (touchState == ZOOM) {
//multi-finger zoom, scale accordingly around center
float dist = calcDistance(event);
if (dist > MIN_DIST) {
matrix.set(eventMatrix);
float scale = dist / eventDistance;
matrix.postScale(scale, scale, centerX, centerY);
}
}
// Perform the transformation
i.setImageMatrix(matrix);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
touchState = NONE;
break;
}
return true;
}
private float calcDistance(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
private void calcMidpoint(float centerX, float centerY,
MotionEvent event) {
centerX = (event.getX(0) + event.getX(1))/2;
centerY = (event.getY(0) + event.getY(1))/2;
}
private ImageSwitcher mSwitcher;
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mThumbIds[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
i.setBackgroundResource(R.drawable.picture_frame);
return i;
}
private Context mContext;
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Rellayout1"
android:layout_width="match_parent" android:layout_height="match_parent" android:isScrollContainer="true">
<RelativeLayout android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:isScrollContainer="true" android:layout_marginBottom="120dp">
<ImageSwitcher android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
>
</ImageSwitcher>
<Gallery android:spacing="16dp" android:layout_height="80dp" android:id="@+id/gallery" android:unselectedAlpha="0.5" android:background="#55000000" android:gravity="center_vertical" android:layout_width="match_parent" android:minHeight="20px"></Gallery>
</RelativeLayout>
<ScrollView
android:id="@+id/ScrollViewSwitcher"
android:layout_width="fill_parent" android:scrollbarAlwaysDrawVerticalTrack="true" android:isScrollContainer="true" android:layout_alignParentBottom="true" android:layout_height="120dp">
<TextView android:scrollbarAlwaysDrawVerticalTrack="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/SwitcherText" android:textSize="18dp"></TextView>
</ScrollView>
</RelativeLayout>
I am trying to enable pinch-zooming on an imageswitcher. I am stuck right now in how to declare the imageview in the ontouch method. I realize that the ImageView i = (Imageview) v is incorrect, I just dont know what to switch it to to declare the imageswitcher imageview in the ontouch...
Here is my java and xml. Thank you for your help!
public class ImageSwitch1 extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory, OnTouchListener {
Matrix matrix = new Matrix();
Matrix eventMatrix = new Matrix();
final static int NONE = 0;
final static int DRAG = 1;
final static int ZOOM = 2;
int touchState = NONE;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.imageswitcher);
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView parent, View v, int position, long id) {
final TextView tv = (TextView)findViewById(R.id.SwitcherText);
mSwitcher.setImageResource(mImageIds[position]);
tv.setText(mText[position]);
}
public void onNothingSelected(AdapterView parent) {
}
public View makeView() {
ImageView i = new ImageView(this);
i.setScaleType(ScaleType.MATRIX);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
i.setOnTouchListener(this);
return i;
}
final static float MIN_DIST = 50;
static float eventDistance = 0;
static float centerX =0, centerY = 0;
public boolean onTouch(View v, MotionEvent event) {
ImageView i = (ImageView) v;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
//primary touch event starts: remember touch down location
touchState = DRAG;
centerX = event.getX(0);
centerY = event.getY(0);
eventMatrix.set(matrix);
break;
case MotionEvent.ACTION_POINTER_DOWN:
//secondary touch event starts: remember distance and center
eventDistance = calcDistance(event);
calcMidpoint(centerX, centerY, event);
if (eventDistance > MIN_DIST) {
eventMatrix.set(matrix);
touchState = ZOOM;
}
break;
case MotionEvent.ACTION_MOVE:
if (touchState == DRAG) {
//single finger drag, translate accordingly
matrix.set(eventMatrix);
matrix.setTranslate(event.getX(0) - centerX,
event.getY(0) - centerY);
} else if (touchState == ZOOM) {
//multi-finger zoom, scale accordingly around center
float dist = calcDistance(event);
if (dist > MIN_DIST) {
matrix.set(eventMatrix);
float scale = dist / eventDistance;
matrix.postScale(scale, scale, centerX, centerY);
}
}
// Perform the transformation
i.setImageMatrix(matrix);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
touchState = NONE;
break;
}
return true;
}
private float calcDistance(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
private void calcMidpoint(float centerX, float centerY,
MotionEvent event) {
centerX = (event.getX(0) + event.getX(1))/2;
centerY = (event.getY(0) + event.getY(1))/2;
}
private ImageSwitcher mSwitcher;
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mThumbIds[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
i.setBackgroundResource(R.drawable.picture_frame);
return i;
}
private Context mContext;
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Rellayout1"
android:layout_width="match_parent" android:layout_height="match_parent" android:isScrollContainer="true">
<RelativeLayout android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:isScrollContainer="true" android:layout_marginBottom="120dp">
<ImageSwitcher android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
>
</ImageSwitcher>
<Gallery android:spacing="16dp" android:layout_height="80dp" android:id="@+id/gallery" android:unselectedAlpha="0.5" android:background="#55000000" android:gravity="center_vertical" android:layout_width="match_parent" android:minHeight="20px"></Gallery>
</RelativeLayout>
<ScrollView
android:id="@+id/ScrollViewSwitcher"
android:layout_width="fill_parent" android:scrollbarAlwaysDrawVerticalTrack="true" android:isScrollContainer="true" android:layout_alignParentBottom="true" android:layout_height="120dp">
<TextView android:scrollbarAlwaysDrawVerticalTrack="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/SwitcherText" android:textSize="18dp"></TextView>
</ScrollView>
</RelativeLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过更改这一行解决了这个问题:
By
但不能使用矩阵,imageSwitcher 不使用矩阵,或者我不知道它是如何完成的。
我希望能帮助你。
I solved this problem changing this line:
By
But can't use matrix, imageSwitcher don't use matrix or i do not know how it's done.
I hope help you with this.