我写了一张SMIL的堆肥器,用于一个班子,我正计划使信管支持输血和输电,这样你就可以把照片和文字放到你们的希望之中。 我先谈一些例子,我自己也做了一些发言,但当我去执行我的项目的拖延和下降时,我没有工作。 主要法典是:
public class ComposerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.composer);
Button add = (Button)findViewById(R.id.addBtn);
...
add.setOnClickListener(mClick);
...
}
OnClickListener mClick = new OnClickListener() {
@Override
public void onClick(View v){
if(v.getId() == R.id.addBtn)
{
FrameLayout fl = (FrameLayout)findViewById(R.id.Canvas);
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.image_add, null);
View image = (View)itemView.findViewById(R.id.image);
image.setBackgroundResource(R.drawable.icon);
fl.addView(itemView, new FrameLayout.LayoutParams(40, 40));
image.setOnTouchListener(drag);
}
...
OnTouchListener drag = new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event){
FrameLayout.LayoutParams par = (LayoutParams) v.getLayoutParams();
switch(v.getId()){//What is being touched
case R.id.image:{//Which action is being taken
switch(event.getAction()){
case MotionEvent.ACTION_MOVE:{
par.topMargin = (int)event.getRawY() - (v.getHeight());
par.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
v.setLayoutParams(par);
break;
}//inner case MOVE
case MotionEvent.ACTION_UP:{
par.height = 40;
par.width = 40;
par.topMargin = (int)event.getRawY() - (v.getHeight());
par.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
v.setLayoutParams(par);
break;
}//inner case UP
case MotionEvent.ACTION_DOWN:{
par.height = 60;
par.width = 60;
v.setLayoutParams(par);
break;
}//inner case UP
}//inner switch
break;
}//case image
}//switch
return true;
}//onTouch
};//drag
}
compos:
...
<FrameLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/TopBar"
android:layout_above="@+id/addBtn"
android:id="@+id/Canvas"
android:layout_gravity="top">
</FrameLayout>
...
和图像:add.xml:
<View android:id="@+id/image"
android:layout_gravity="top"
xmlns:android="http://schemas.android.com/apk/res/android"/>
当我点击我的 compos子时,它成功地为信.增添了形象,当我触及图像时,它的反应是,像它一样,从40x40到60x60。 但是,它没有听从我的指手,也没有我 st的。
Any input is appreciated.